1//===- ASTReader.cpp - AST File Reader ------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ASTReader class, which reads AST files.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Basic/OpenMPKinds.h"
14#include "clang/Serialization/ASTRecordReader.h"
15#include "ASTCommon.h"
16#include "ASTReaderInternals.h"
17#include "clang/AST/AbstractTypeReader.h"
18#include "clang/AST/ASTConsumer.h"
19#include "clang/AST/ASTContext.h"
20#include "clang/AST/ASTMutationListener.h"
21#include "clang/AST/ASTUnresolvedSet.h"
22#include "clang/AST/Decl.h"
23#include "clang/AST/DeclBase.h"
24#include "clang/AST/DeclCXX.h"
25#include "clang/AST/DeclFriend.h"
26#include "clang/AST/DeclGroup.h"
27#include "clang/AST/DeclObjC.h"
28#include "clang/AST/DeclTemplate.h"
29#include "clang/AST/DeclarationName.h"
30#include "clang/AST/Expr.h"
31#include "clang/AST/ExprCXX.h"
32#include "clang/AST/ExternalASTSource.h"
33#include "clang/AST/NestedNameSpecifier.h"
34#include "clang/AST/OpenMPClause.h"
35#include "clang/AST/ODRHash.h"
36#include "clang/AST/RawCommentList.h"
37#include "clang/AST/TemplateBase.h"
38#include "clang/AST/TemplateName.h"
39#include "clang/AST/Type.h"
40#include "clang/AST/TypeLoc.h"
41#include "clang/AST/TypeLocVisitor.h"
42#include "clang/AST/UnresolvedSet.h"
43#include "clang/Basic/CommentOptions.h"
44#include "clang/Basic/Diagnostic.h"
45#include "clang/Basic/DiagnosticOptions.h"
46#include "clang/Basic/ExceptionSpecificationType.h"
47#include "clang/Basic/FileManager.h"
48#include "clang/Basic/FileSystemOptions.h"
49#include "clang/Basic/IdentifierTable.h"
50#include "clang/Basic/LLVM.h"
51#include "clang/Basic/LangOptions.h"
52#include "clang/Basic/Module.h"
53#include "clang/Basic/ObjCRuntime.h"
54#include "clang/Basic/OperatorKinds.h"
55#include "clang/Basic/PragmaKinds.h"
56#include "clang/Basic/Sanitizers.h"
57#include "clang/Basic/SourceLocation.h"
58#include "clang/Basic/SourceManager.h"
59#include "clang/Basic/SourceManagerInternals.h"
60#include "clang/Basic/Specifiers.h"
61#include "clang/Basic/TargetInfo.h"
62#include "clang/Basic/TargetOptions.h"
63#include "clang/Basic/TokenKinds.h"
64#include "clang/Basic/Version.h"
65#include "clang/Lex/HeaderSearch.h"
66#include "clang/Lex/HeaderSearchOptions.h"
67#include "clang/Lex/MacroInfo.h"
68#include "clang/Lex/ModuleMap.h"
69#include "clang/Lex/PreprocessingRecord.h"
70#include "clang/Lex/Preprocessor.h"
71#include "clang/Lex/PreprocessorOptions.h"
72#include "clang/Lex/Token.h"
73#include "clang/Sema/ObjCMethodList.h"
74#include "clang/Sema/Scope.h"
75#include "clang/Sema/Sema.h"
76#include "clang/Sema/Weak.h"
77#include "clang/Serialization/ASTBitCodes.h"
78#include "clang/Serialization/ASTDeserializationListener.h"
79#include "clang/Serialization/ContinuousRangeMap.h"
80#include "clang/Serialization/GlobalModuleIndex.h"
81#include "clang/Serialization/InMemoryModuleCache.h"
82#include "clang/Serialization/ModuleFile.h"
83#include "clang/Serialization/ModuleFileExtension.h"
84#include "clang/Serialization/ModuleManager.h"
85#include "clang/Serialization/PCHContainerOperations.h"
86#include "clang/Serialization/SerializationDiagnostic.h"
87#include "llvm/ADT/APFloat.h"
88#include "llvm/ADT/APInt.h"
89#include "llvm/ADT/APSInt.h"
90#include "llvm/ADT/ArrayRef.h"
91#include "llvm/ADT/DenseMap.h"
92#include "llvm/ADT/FloatingPointMode.h"
93#include "llvm/ADT/FoldingSet.h"
94#include "llvm/ADT/Hashing.h"
95#include "llvm/ADT/IntrusiveRefCntPtr.h"
96#include "llvm/ADT/None.h"
97#include "llvm/ADT/Optional.h"
98#include "llvm/ADT/STLExtras.h"
99#include "llvm/ADT/ScopeExit.h"
100#include "llvm/ADT/SmallPtrSet.h"
101#include "llvm/ADT/SmallString.h"
102#include "llvm/ADT/SmallVector.h"
103#include "llvm/ADT/StringExtras.h"
104#include "llvm/ADT/StringMap.h"
105#include "llvm/ADT/StringRef.h"
106#include "llvm/ADT/Triple.h"
107#include "llvm/ADT/iterator_range.h"
108#include "llvm/Bitstream/BitstreamReader.h"
109#include "llvm/Support/Casting.h"
110#include "llvm/Support/Compiler.h"
111#include "llvm/Support/Compression.h"
112#include "llvm/Support/DJB.h"
113#include "llvm/Support/Endian.h"
114#include "llvm/Support/Error.h"
115#include "llvm/Support/ErrorHandling.h"
116#include "llvm/Support/FileSystem.h"
117#include "llvm/Support/MemoryBuffer.h"
118#include "llvm/Support/Path.h"
119#include "llvm/Support/SaveAndRestore.h"
120#include "llvm/Support/Timer.h"
121#include "llvm/Support/VersionTuple.h"
122#include "llvm/Support/raw_ostream.h"
123#include <algorithm>
124#include <cassert>
125#include <cstddef>
126#include <cstdint>
127#include <cstdio>
128#include <ctime>
129#include <iterator>
130#include <limits>
131#include <map>
132#include <memory>
133#include <string>
134#include <system_error>
135#include <tuple>
136#include <utility>
137#include <vector>
138
139using namespace clang;
140using namespace clang::serialization;
141using namespace clang::serialization::reader;
142using llvm::BitstreamCursor;
143using llvm::RoundingMode;
144
145//===----------------------------------------------------------------------===//
146// ChainedASTReaderListener implementation
147//===----------------------------------------------------------------------===//
148
149bool
150ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
151 return First->ReadFullVersionInformation(FullVersion) ||
152 Second->ReadFullVersionInformation(FullVersion);
153}
154
155void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
156 First->ReadModuleName(ModuleName);
157 Second->ReadModuleName(ModuleName);
158}
159
160void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
161 First->ReadModuleMapFile(ModuleMapPath);
162 Second->ReadModuleMapFile(ModuleMapPath);
163}
164
165bool
166ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
167 bool Complain,
168 bool AllowCompatibleDifferences) {
169 return First->ReadLanguageOptions(LangOpts, Complain,
170 AllowCompatibleDifferences) ||
171 Second->ReadLanguageOptions(LangOpts, Complain,
172 AllowCompatibleDifferences);
173}
174
175bool ChainedASTReaderListener::ReadTargetOptions(
176 const TargetOptions &TargetOpts, bool Complain,
177 bool AllowCompatibleDifferences) {
178 return First->ReadTargetOptions(TargetOpts, Complain,
179 AllowCompatibleDifferences) ||
180 Second->ReadTargetOptions(TargetOpts, Complain,
181 AllowCompatibleDifferences);
182}
183
184bool ChainedASTReaderListener::ReadDiagnosticOptions(
185 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
186 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
187 Second->ReadDiagnosticOptions(DiagOpts, Complain);
188}
189
190bool
191ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
192 bool Complain) {
193 return First->ReadFileSystemOptions(FSOpts, Complain) ||
194 Second->ReadFileSystemOptions(FSOpts, Complain);
195}
196
197bool ChainedASTReaderListener::ReadHeaderSearchOptions(
198 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
199 bool Complain) {
200 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
201 Complain) ||
202 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
203 Complain);
204}
205
206bool ChainedASTReaderListener::ReadPreprocessorOptions(
207 const PreprocessorOptions &PPOpts, bool Complain,
208 std::string &SuggestedPredefines) {
209 return First->ReadPreprocessorOptions(PPOpts, Complain,
210 SuggestedPredefines) ||
211 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
212}
213
214void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
215 unsigned Value) {
216 First->ReadCounter(M, Value);
217 Second->ReadCounter(M, Value);
218}
219
220bool ChainedASTReaderListener::needsInputFileVisitation() {
221 return First->needsInputFileVisitation() ||
222 Second->needsInputFileVisitation();
223}
224
225bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
226 return First->needsSystemInputFileVisitation() ||
227 Second->needsSystemInputFileVisitation();
228}
229
230void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
231 ModuleKind Kind) {
232 First->visitModuleFile(Filename, Kind);
233 Second->visitModuleFile(Filename, Kind);
234}
235
236bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
237 bool isSystem,
238 bool isOverridden,
239 bool isExplicitModule) {
240 bool Continue = false;
241 if (First->needsInputFileVisitation() &&
242 (!isSystem || First->needsSystemInputFileVisitation()))
243 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
244 isExplicitModule);
245 if (Second->needsInputFileVisitation() &&
246 (!isSystem || Second->needsSystemInputFileVisitation()))
247 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
248 isExplicitModule);
249 return Continue;
250}
251
252void ChainedASTReaderListener::readModuleFileExtension(
253 const ModuleFileExtensionMetadata &Metadata) {
254 First->readModuleFileExtension(Metadata);
255 Second->readModuleFileExtension(Metadata);
256}
257
258//===----------------------------------------------------------------------===//
259// PCH validator implementation
260//===----------------------------------------------------------------------===//
261
262ASTReaderListener::~ASTReaderListener() = default;
263
264/// Compare the given set of language options against an existing set of
265/// language options.
266///
267/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
268/// \param AllowCompatibleDifferences If true, differences between compatible
269/// language options will be permitted.
270///
271/// \returns true if the languagae options mis-match, false otherwise.
272static bool checkLanguageOptions(const LangOptions &LangOpts,
273 const LangOptions &ExistingLangOpts,
274 DiagnosticsEngine *Diags,
275 bool AllowCompatibleDifferences = true) {
276#define LANGOPT(Name, Bits, Default, Description) \
277 if (ExistingLangOpts.Name != LangOpts.Name) { \
278 if (Diags) \
279 Diags->Report(diag::err_pch_langopt_mismatch) \
280 << Description << LangOpts.Name << ExistingLangOpts.Name; \
281 return true; \
282 }
283
284#define VALUE_LANGOPT(Name, Bits, Default, Description) \
285 if (ExistingLangOpts.Name != LangOpts.Name) { \
286 if (Diags) \
287 Diags->Report(diag::err_pch_langopt_value_mismatch) \
288 << Description; \
289 return true; \
290 }
291
292#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
293 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
294 if (Diags) \
295 Diags->Report(diag::err_pch_langopt_value_mismatch) \
296 << Description; \
297 return true; \
298 }
299
300#define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
301 if (!AllowCompatibleDifferences) \
302 LANGOPT(Name, Bits, Default, Description)
303
304#define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
305 if (!AllowCompatibleDifferences) \
306 ENUM_LANGOPT(Name, Bits, Default, Description)
307
308#define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
309 if (!AllowCompatibleDifferences) \
310 VALUE_LANGOPT(Name, Bits, Default, Description)
311
312#define BENIGN_LANGOPT(Name, Bits, Default, Description)
313#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
314#define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
315#include "clang/Basic/LangOptions.def"
316
317 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
318 if (Diags)
319 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
320 return true;
321 }
322
323 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
324 if (Diags)
325 Diags->Report(diag::err_pch_langopt_value_mismatch)
326 << "target Objective-C runtime";
327 return true;
328 }
329
330 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
331 LangOpts.CommentOpts.BlockCommandNames) {
332 if (Diags)
333 Diags->Report(diag::err_pch_langopt_value_mismatch)
334 << "block command names";
335 return true;
336 }
337
338 // Sanitizer feature mismatches are treated as compatible differences. If
339 // compatible differences aren't allowed, we still only want to check for
340 // mismatches of non-modular sanitizers (the only ones which can affect AST
341 // generation).
342 if (!AllowCompatibleDifferences) {
343 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
344 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
345 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
346 ExistingSanitizers.clear(ModularSanitizers);
347 ImportedSanitizers.clear(ModularSanitizers);
348 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
349 const std::string Flag = "-fsanitize=";
350 if (Diags) {
351#define SANITIZER(NAME, ID) \
352 { \
353 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
354 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
355 if (InExistingModule != InImportedModule) \
356 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \
357 << InExistingModule << (Flag + NAME); \
358 }
359#include "clang/Basic/Sanitizers.def"
360 }
361 return true;
362 }
363 }
364
365 return false;
366}
367
368/// Compare the given set of target options against an existing set of
369/// target options.
370///
371/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
372///
373/// \returns true if the target options mis-match, false otherwise.
374static bool checkTargetOptions(const TargetOptions &TargetOpts,
375 const TargetOptions &ExistingTargetOpts,
376 DiagnosticsEngine *Diags,
377 bool AllowCompatibleDifferences = true) {
378#define CHECK_TARGET_OPT(Field, Name) \
379 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
380 if (Diags) \
381 Diags->Report(diag::err_pch_targetopt_mismatch) \
382 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
383 return true; \
384 }
385
386 // The triple and ABI must match exactly.
387 CHECK_TARGET_OPT(Triple, "target");
388 CHECK_TARGET_OPT(ABI, "target ABI");
389
390 // We can tolerate different CPUs in many cases, notably when one CPU
391 // supports a strict superset of another. When allowing compatible
392 // differences skip this check.
393 if (!AllowCompatibleDifferences) {
394 CHECK_TARGET_OPT(CPU, "target CPU");
395 CHECK_TARGET_OPT(TuneCPU, "tune CPU");
396 }
397
398#undef CHECK_TARGET_OPT
399
400 // Compare feature sets.
401 SmallVector<StringRef, 4> ExistingFeatures(
402 ExistingTargetOpts.FeaturesAsWritten.begin(),
403 ExistingTargetOpts.FeaturesAsWritten.end());
404 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
405 TargetOpts.FeaturesAsWritten.end());
406 llvm::sort(ExistingFeatures);
407 llvm::sort(ReadFeatures);
408
409 // We compute the set difference in both directions explicitly so that we can
410 // diagnose the differences differently.
411 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
412 std::set_difference(
413 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
414 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
415 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
416 ExistingFeatures.begin(), ExistingFeatures.end(),
417 std::back_inserter(UnmatchedReadFeatures));
418
419 // If we are allowing compatible differences and the read feature set is
420 // a strict subset of the existing feature set, there is nothing to diagnose.
421 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
422 return false;
423
424 if (Diags) {
425 for (StringRef Feature : UnmatchedReadFeatures)
426 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
427 << /* is-existing-feature */ false << Feature;
428 for (StringRef Feature : UnmatchedExistingFeatures)
429 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
430 << /* is-existing-feature */ true << Feature;
431 }
432
433 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
434}
435
436bool
437PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
438 bool Complain,
439 bool AllowCompatibleDifferences) {
440 const LangOptions &ExistingLangOpts = PP.getLangOpts();
441 return checkLanguageOptions(LangOpts, ExistingLangOpts,
442 Complain ? &Reader.Diags : nullptr,
443 AllowCompatibleDifferences);
444}
445
446bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
447 bool Complain,
448 bool AllowCompatibleDifferences) {
449 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
450 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
451 Complain ? &Reader.Diags : nullptr,
452 AllowCompatibleDifferences);
453}
454
455namespace {
456
457using MacroDefinitionsMap =
458 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
459using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
460
461} // namespace
462
463static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
464 DiagnosticsEngine &Diags,
465 bool Complain) {
466 using Level = DiagnosticsEngine::Level;
467
468 // Check current mappings for new -Werror mappings, and the stored mappings
469 // for cases that were explicitly mapped to *not* be errors that are now
470 // errors because of options like -Werror.
471 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
472
473 for (DiagnosticsEngine *MappingSource : MappingSources) {
474 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
475 diag::kind DiagID = DiagIDMappingPair.first;
476 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
477 if (CurLevel < DiagnosticsEngine::Error)
478 continue; // not significant
479 Level StoredLevel =
480 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
481 if (StoredLevel < DiagnosticsEngine::Error) {
482 if (Complain)
483 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
484 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
485 return true;
486 }
487 }
488 }
489
490 return false;
491}
492
493static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
494 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
495 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
496 return true;
497 return Ext >= diag::Severity::Error;
498}
499
500static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
501 DiagnosticsEngine &Diags,
502 bool IsSystem, bool Complain) {
503 // Top-level options
504 if (IsSystem) {
505 if (Diags.getSuppressSystemWarnings())
506 return false;
507 // If -Wsystem-headers was not enabled before, be conservative
508 if (StoredDiags.getSuppressSystemWarnings()) {
509 if (Complain)
510 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
511 return true;
512 }
513 }
514
515 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
516 if (Complain)
517 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
518 return true;
519 }
520
521 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
522 !StoredDiags.getEnableAllWarnings()) {
523 if (Complain)
524 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
525 return true;
526 }
527
528 if (isExtHandlingFromDiagsError(Diags) &&
529 !isExtHandlingFromDiagsError(StoredDiags)) {
530 if (Complain)
531 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
532 return true;
533 }
534
535 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
536}
537
538/// Return the top import module if it is implicit, nullptr otherwise.
539static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
540 Preprocessor &PP) {
541 // If the original import came from a file explicitly generated by the user,
542 // don't check the diagnostic mappings.
543 // FIXME: currently this is approximated by checking whether this is not a
544 // module import of an implicitly-loaded module file.
545 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
546 // the transitive closure of its imports, since unrelated modules cannot be
547 // imported until after this module finishes validation.
548 ModuleFile *TopImport = &*ModuleMgr.rbegin();
549 while (!TopImport->ImportedBy.empty())
550 TopImport = TopImport->ImportedBy[0];
551 if (TopImport->Kind != MK_ImplicitModule)
552 return nullptr;
553
554 StringRef ModuleName = TopImport->ModuleName;
555 assert(!ModuleName.empty() && "diagnostic options read before module name");
556
557 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
558 assert(M && "missing module");
559 return M;
560}
561
562bool PCHValidator::ReadDiagnosticOptions(
563 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
564 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
565 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
566 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
567 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
568 // This should never fail, because we would have processed these options
569 // before writing them to an ASTFile.
570 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
571
572 ModuleManager &ModuleMgr = Reader.getModuleManager();
573 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
574
575 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
576 if (!TopM)
577 return false;
578
579 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
580 // contains the union of their flags.
581 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
582 Complain);
583}
584
585/// Collect the macro definitions provided by the given preprocessor
586/// options.
587static void
588collectMacroDefinitions(const PreprocessorOptions &PPOpts,
589 MacroDefinitionsMap &Macros,
590 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
591 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
592 StringRef Macro = PPOpts.Macros[I].first;
593 bool IsUndef = PPOpts.Macros[I].second;
594
595 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
596 StringRef MacroName = MacroPair.first;
597 StringRef MacroBody = MacroPair.second;
598
599 // For an #undef'd macro, we only care about the name.
600 if (IsUndef) {
601 if (MacroNames && !Macros.count(MacroName))
602 MacroNames->push_back(MacroName);
603
604 Macros[MacroName] = std::make_pair("", true);
605 continue;
606 }
607
608 // For a #define'd macro, figure out the actual definition.
609 if (MacroName.size() == Macro.size())
610 MacroBody = "1";
611 else {
612 // Note: GCC drops anything following an end-of-line character.
613 StringRef::size_type End = MacroBody.find_first_of("\n\r");
614 MacroBody = MacroBody.substr(0, End);
615 }
616
617 if (MacroNames && !Macros.count(MacroName))
618 MacroNames->push_back(MacroName);
619 Macros[MacroName] = std::make_pair(MacroBody, false);
620 }
621}
622
623/// Check the preprocessor options deserialized from the control block
624/// against the preprocessor options in an existing preprocessor.
625///
626/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
627/// \param Validate If true, validate preprocessor options. If false, allow
628/// macros defined by \p ExistingPPOpts to override those defined by
629/// \p PPOpts in SuggestedPredefines.
630static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
631 const PreprocessorOptions &ExistingPPOpts,
632 DiagnosticsEngine *Diags,
633 FileManager &FileMgr,
634 std::string &SuggestedPredefines,
635 const LangOptions &LangOpts,
636 bool Validate = true) {
637 // Check macro definitions.
638 MacroDefinitionsMap ASTFileMacros;
639 collectMacroDefinitions(PPOpts, ASTFileMacros);
640 MacroDefinitionsMap ExistingMacros;
641 SmallVector<StringRef, 4> ExistingMacroNames;
642 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
643
644 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
645 // Dig out the macro definition in the existing preprocessor options.
646 StringRef MacroName = ExistingMacroNames[I];
647 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
648
649 // Check whether we know anything about this macro name or not.
650 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
651 ASTFileMacros.find(MacroName);
652 if (!Validate || Known == ASTFileMacros.end()) {
653 // FIXME: Check whether this identifier was referenced anywhere in the
654 // AST file. If so, we should reject the AST file. Unfortunately, this
655 // information isn't in the control block. What shall we do about it?
656
657 if (Existing.second) {
658 SuggestedPredefines += "#undef ";
659 SuggestedPredefines += MacroName.str();
660 SuggestedPredefines += '\n';
661 } else {
662 SuggestedPredefines += "#define ";
663 SuggestedPredefines += MacroName.str();
664 SuggestedPredefines += ' ';
665 SuggestedPredefines += Existing.first.str();
666 SuggestedPredefines += '\n';
667 }
668 continue;
669 }
670
671 // If the macro was defined in one but undef'd in the other, we have a
672 // conflict.
673 if (Existing.second != Known->second.second) {
674 if (Diags) {
675 Diags->Report(diag::err_pch_macro_def_undef)
676 << MacroName << Known->second.second;
677 }
678 return true;
679 }
680
681 // If the macro was #undef'd in both, or if the macro bodies are identical,
682 // it's fine.
683 if (Existing.second || Existing.first == Known->second.first)
684 continue;
685
686 // The macro bodies differ; complain.
687 if (Diags) {
688 Diags->Report(diag::err_pch_macro_def_conflict)
689 << MacroName << Known->second.first << Existing.first;
690 }
691 return true;
692 }
693
694 // Check whether we're using predefines.
695 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
696 if (Diags) {
697 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
698 }
699 return true;
700 }
701
702 // Detailed record is important since it is used for the module cache hash.
703 if (LangOpts.Modules &&
704 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
705 if (Diags) {
706 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
707 }
708 return true;
709 }
710
711 // Compute the #include and #include_macros lines we need.
712 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
713 StringRef File = ExistingPPOpts.Includes[I];
714
715 if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
716 !ExistingPPOpts.PCHThroughHeader.empty()) {
717 // In case the through header is an include, we must add all the includes
718 // to the predefines so the start point can be determined.
719 SuggestedPredefines += "#include \"";
720 SuggestedPredefines += File;
721 SuggestedPredefines += "\"\n";
722 continue;
723 }
724
725 if (File == ExistingPPOpts.ImplicitPCHInclude)
726 continue;
727
728 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
729 != PPOpts.Includes.end())
730 continue;
731
732 SuggestedPredefines += "#include \"";
733 SuggestedPredefines += File;
734 SuggestedPredefines += "\"\n";
735 }
736
737 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
738 StringRef File = ExistingPPOpts.MacroIncludes[I];
739 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
740 File)
741 != PPOpts.MacroIncludes.end())
742 continue;
743
744 SuggestedPredefines += "#__include_macros \"";
745 SuggestedPredefines += File;
746 SuggestedPredefines += "\"\n##\n";
747 }
748
749 return false;
750}
751
752bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
753 bool Complain,
754 std::string &SuggestedPredefines) {
755 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
756
757 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
758 Complain? &Reader.Diags : nullptr,
759 PP.getFileManager(),
760 SuggestedPredefines,
761 PP.getLangOpts());
762}
763
764bool SimpleASTReaderListener::ReadPreprocessorOptions(
765 const PreprocessorOptions &PPOpts,
766 bool Complain,
767 std::string &SuggestedPredefines) {
768 return checkPreprocessorOptions(PPOpts,
769 PP.getPreprocessorOpts(),
770 nullptr,
771 PP.getFileManager(),
772 SuggestedPredefines,
773 PP.getLangOpts(),
774 false);
775}
776
777/// Check the header search options deserialized from the control block
778/// against the header search options in an existing preprocessor.
779///
780/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
781static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
782 StringRef SpecificModuleCachePath,
783 StringRef ExistingModuleCachePath,
784 DiagnosticsEngine *Diags,
785 const LangOptions &LangOpts) {
786 if (LangOpts.Modules) {
787 if (SpecificModuleCachePath != ExistingModuleCachePath) {
788 if (Diags)
789 Diags->Report(diag::err_pch_modulecache_mismatch)
790 << SpecificModuleCachePath << ExistingModuleCachePath;
791 return true;
792 }
793 }
794
795 return false;
796}
797
798bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
799 StringRef SpecificModuleCachePath,
800 bool Complain) {
801 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
802 PP.getHeaderSearchInfo().getModuleCachePath(),
803 Complain ? &Reader.Diags : nullptr,
804 PP.getLangOpts());
805}
806
807void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
808 PP.setCounterValue(Value);
809}
810
811//===----------------------------------------------------------------------===//
812// AST reader implementation
813//===----------------------------------------------------------------------===//
814
815void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
816 bool TakeOwnership) {
817 DeserializationListener = Listener;
818 OwnsDeserializationListener = TakeOwnership;
819}
820
821unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
822 return serialization::ComputeHash(Sel);
823}
824
825std::pair<unsigned, unsigned>
826ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
827 using namespace llvm::support;
828
829 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
830 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
831 return std::make_pair(KeyLen, DataLen);
832}
833
834ASTSelectorLookupTrait::internal_key_type
835ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
836 using namespace llvm::support;
837
838 SelectorTable &SelTable = Reader.getContext().Selectors;
839 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
840 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
841 F, endian::readNext<uint32_t, little, unaligned>(d));
842 if (N == 0)
843 return SelTable.getNullarySelector(FirstII);
844 else if (N == 1)
845 return SelTable.getUnarySelector(FirstII);
846
847 SmallVector<IdentifierInfo *, 16> Args;
848 Args.push_back(FirstII);
849 for (unsigned I = 1; I != N; ++I)
850 Args.push_back(Reader.getLocalIdentifier(
851 F, endian::readNext<uint32_t, little, unaligned>(d)));
852
853 return SelTable.getSelector(N, Args.data());
854}
855
856ASTSelectorLookupTrait::data_type
857ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
858 unsigned DataLen) {
859 using namespace llvm::support;
860
861 data_type Result;
862
863 Result.ID = Reader.getGlobalSelectorID(
864 F, endian::readNext<uint32_t, little, unaligned>(d));
865 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
866 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
867 Result.InstanceBits = FullInstanceBits & 0x3;
868 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
869 Result.FactoryBits = FullFactoryBits & 0x3;
870 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
871 unsigned NumInstanceMethods = FullInstanceBits >> 3;
872 unsigned NumFactoryMethods = FullFactoryBits >> 3;
873
874 // Load instance methods
875 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
876 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
877 F, endian::readNext<uint32_t, little, unaligned>(d)))
878 Result.Instance.push_back(Method);
879 }
880
881 // Load factory methods
882 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
883 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
884 F, endian::readNext<uint32_t, little, unaligned>(d)))
885 Result.Factory.push_back(Method);
886 }
887
888 return Result;
889}
890
891unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
892 return llvm::djbHash(a);
893}
894
895std::pair<unsigned, unsigned>
896ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
897 using namespace llvm::support;
898
899 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
900 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
901 return std::make_pair(KeyLen, DataLen);
902}
903
904ASTIdentifierLookupTraitBase::internal_key_type
905ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
906 assert(n >= 2 && d[n-1] == '\0');
907 return StringRef((const char*) d, n-1);
908}
909
910/// Whether the given identifier is "interesting".
911static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
912 bool IsModule) {
913 return II.hadMacroDefinition() || II.isPoisoned() ||
914 (!IsModule && II.getObjCOrBuiltinID()) ||
915 II.hasRevertedTokenIDToIdentifier() ||
916 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
917 II.getFETokenInfo());
918}
919
920static bool readBit(unsigned &Bits) {
921 bool Value = Bits & 0x1;
922 Bits >>= 1;
923 return Value;
924}
925
926IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
927 using namespace llvm::support;
928
929 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
930 return Reader.getGlobalIdentifierID(F, RawID >> 1);
931}
932
933static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
934 if (!II.isFromAST()) {
935 II.setIsFromAST();
936 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
937 if (isInterestingIdentifier(Reader, II, IsModule))
938 II.setChangedSinceDeserialization();
939 }
940}
941
942IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
943 const unsigned char* d,
944 unsigned DataLen) {
945 using namespace llvm::support;
946
947 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
948 bool IsInteresting = RawID & 0x01;
949
950 // Wipe out the "is interesting" bit.
951 RawID = RawID >> 1;
952
953 // Build the IdentifierInfo and link the identifier ID with it.
954 IdentifierInfo *II = KnownII;
955 if (!II) {
956 II = &Reader.getIdentifierTable().getOwn(k);
957 KnownII = II;
958 }
959 markIdentifierFromAST(Reader, *II);
960 Reader.markIdentifierUpToDate(II);
961
962 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
963 if (!IsInteresting) {
964 // For uninteresting identifiers, there's nothing else to do. Just notify
965 // the reader that we've finished loading this identifier.
966 Reader.SetIdentifierInfo(ID, II);
967 return II;
968 }
969
970 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
971 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
972 bool CPlusPlusOperatorKeyword = readBit(Bits);
973 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
974 bool Poisoned = readBit(Bits);
975 bool ExtensionToken = readBit(Bits);
976 bool HadMacroDefinition = readBit(Bits);
977
978 assert(Bits == 0 && "Extra bits in the identifier?");
979 DataLen -= 8;
980
981 // Set or check the various bits in the IdentifierInfo structure.
982 // Token IDs are read-only.
983 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
984 II->revertTokenIDToIdentifier();
985 if (!F.isModule())
986 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
987 assert(II->isExtensionToken() == ExtensionToken &&
988 "Incorrect extension token flag");
989 (void)ExtensionToken;
990 if (Poisoned)
991 II->setIsPoisoned(true);
992 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
993 "Incorrect C++ operator keyword flag");
994 (void)CPlusPlusOperatorKeyword;
995
996 // If this identifier is a macro, deserialize the macro
997 // definition.
998 if (HadMacroDefinition) {
999 uint32_t MacroDirectivesOffset =
1000 endian::readNext<uint32_t, little, unaligned>(d);
1001 DataLen -= 4;
1002
1003 Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1004 }
1005
1006 Reader.SetIdentifierInfo(ID, II);
1007
1008 // Read all of the declarations visible at global scope with this
1009 // name.
1010 if (DataLen > 0) {
1011 SmallVector<uint32_t, 4> DeclIDs;
1012 for (; DataLen > 0; DataLen -= 4)
1013 DeclIDs.push_back(Reader.getGlobalDeclID(
1014 F, endian::readNext<uint32_t, little, unaligned>(d)));
1015 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1016 }
1017
1018 return II;
1019}
1020
1021DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1022 : Kind(Name.getNameKind()) {
1023 switch (Kind) {
1024 case DeclarationName::Identifier:
1025 Data = (uint64_t)Name.getAsIdentifierInfo();
1026 break;
1027 case DeclarationName::ObjCZeroArgSelector:
1028 case DeclarationName::ObjCOneArgSelector:
1029 case DeclarationName::ObjCMultiArgSelector:
1030 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1031 break;
1032 case DeclarationName::CXXOperatorName:
1033 Data = Name.getCXXOverloadedOperator();
1034 break;
1035 case DeclarationName::CXXLiteralOperatorName:
1036 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1037 break;
1038 case DeclarationName::CXXDeductionGuideName:
1039 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1040 ->getDeclName().getAsIdentifierInfo();
1041 break;
1042 case DeclarationName::CXXConstructorName:
1043 case DeclarationName::CXXDestructorName:
1044 case DeclarationName::CXXConversionFunctionName:
1045 case DeclarationName::CXXUsingDirective:
1046 Data = 0;
1047 break;
1048 }
1049}
1050
1051unsigned DeclarationNameKey::getHash() const {
1052 llvm::FoldingSetNodeID ID;
1053 ID.AddInteger(Kind);
1054
1055 switch (Kind) {
1056 case DeclarationName::Identifier:
1057 case DeclarationName::CXXLiteralOperatorName:
1058 case DeclarationName::CXXDeductionGuideName:
1059 ID.AddString(((IdentifierInfo*)Data)->getName());
1060 break;
1061 case DeclarationName::ObjCZeroArgSelector:
1062 case DeclarationName::ObjCOneArgSelector:
1063 case DeclarationName::ObjCMultiArgSelector:
1064 ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1065 break;
1066 case DeclarationName::CXXOperatorName:
1067 ID.AddInteger((OverloadedOperatorKind)Data);
1068 break;
1069 case DeclarationName::CXXConstructorName:
1070 case DeclarationName::CXXDestructorName:
1071 case DeclarationName::CXXConversionFunctionName:
1072 case DeclarationName::CXXUsingDirective:
1073 break;
1074 }
1075
1076 return ID.ComputeHash();
1077}
1078
1079ModuleFile *
1080ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1081 using namespace llvm::support;
1082
1083 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1084 return Reader.getLocalModuleFile(F, ModuleFileID);
1085}
1086
1087std::pair<unsigned, unsigned>
1088ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1089 using namespace llvm::support;
1090
1091 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1092 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1093 return std::make_pair(KeyLen, DataLen);
1094}
1095
1096ASTDeclContextNameLookupTrait::internal_key_type
1097ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1098 using namespace llvm::support;
1099
1100 auto Kind = (DeclarationName::NameKind)*d++;
1101 uint64_t Data;
1102 switch (Kind) {
1103 case DeclarationName::Identifier:
1104 case DeclarationName::CXXLiteralOperatorName:
1105 case DeclarationName::CXXDeductionGuideName:
1106 Data = (uint64_t)Reader.getLocalIdentifier(
1107 F, endian::readNext<uint32_t, little, unaligned>(d));
1108 break;
1109 case DeclarationName::ObjCZeroArgSelector:
1110 case DeclarationName::ObjCOneArgSelector:
1111 case DeclarationName::ObjCMultiArgSelector:
1112 Data =
1113 (uint64_t)Reader.getLocalSelector(
1114 F, endian::readNext<uint32_t, little, unaligned>(
1115 d)).getAsOpaquePtr();
1116 break;
1117 case DeclarationName::CXXOperatorName:
1118 Data = *d++; // OverloadedOperatorKind
1119 break;
1120 case DeclarationName::CXXConstructorName:
1121 case DeclarationName::CXXDestructorName:
1122 case DeclarationName::CXXConversionFunctionName:
1123 case DeclarationName::CXXUsingDirective:
1124 Data = 0;
1125 break;
1126 }
1127
1128 return DeclarationNameKey(Kind, Data);
1129}
1130
1131void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1132 const unsigned char *d,
1133 unsigned DataLen,
1134 data_type_builder &Val) {
1135 using namespace llvm::support;
1136
1137 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1138 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1139 Val.insert(Reader.getGlobalDeclID(F, LocalID));
1140 }
1141}
1142
1143bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1144 BitstreamCursor &Cursor,
1145 uint64_t Offset,
1146 DeclContext *DC) {
1147 assert(Offset != 0);
1148
1149 SavedStreamPosition SavedPosition(Cursor);
1150 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1151 Error(std::move(Err));
1152 return true;
1153 }
1154
1155 RecordData Record;
1156 StringRef Blob;
1157 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1158 if (!MaybeCode) {
1159 Error(MaybeCode.takeError());
1160 return true;
1161 }
1162 unsigned Code = MaybeCode.get();
1163
1164 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1165 if (!MaybeRecCode) {
1166 Error(MaybeRecCode.takeError());
1167 return true;
1168 }
1169 unsigned RecCode = MaybeRecCode.get();
1170 if (RecCode != DECL_CONTEXT_LEXICAL) {
1171 Error("Expected lexical block");
1172 return true;
1173 }
1174
1175 assert(!isa<TranslationUnitDecl>(DC) &&
1176 "expected a TU_UPDATE_LEXICAL record for TU");
1177 // If we are handling a C++ class template instantiation, we can see multiple
1178 // lexical updates for the same record. It's important that we select only one
1179 // of them, so that field numbering works properly. Just pick the first one we
1180 // see.
1181 auto &Lex = LexicalDecls[DC];
1182 if (!Lex.first) {
1183 Lex = std::make_pair(
1184 &M, llvm::makeArrayRef(
1185 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1186 Blob.data()),
1187 Blob.size() / 4));
1188 }
1189 DC->setHasExternalLexicalStorage(true);
1190 return false;
1191}
1192
1193bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1194 BitstreamCursor &Cursor,
1195 uint64_t Offset,
1196 DeclID ID) {
1197 assert(Offset != 0);
1198
1199 SavedStreamPosition SavedPosition(Cursor);
1200 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1201 Error(std::move(Err));
1202 return true;
1203 }
1204
1205 RecordData Record;
1206 StringRef Blob;
1207 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1208 if (!MaybeCode) {
1209 Error(MaybeCode.takeError());
1210 return true;
1211 }
1212 unsigned Code = MaybeCode.get();
1213
1214 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1215 if (!MaybeRecCode) {
1216 Error(MaybeRecCode.takeError());
1217 return true;
1218 }
1219 unsigned RecCode = MaybeRecCode.get();
1220 if (RecCode != DECL_CONTEXT_VISIBLE) {
1221 Error("Expected visible lookup table block");
1222 return true;
1223 }
1224
1225 // We can't safely determine the primary context yet, so delay attaching the
1226 // lookup table until we're done with recursive deserialization.
1227 auto *Data = (const unsigned char*)Blob.data();
1228 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1229 return false;
1230}
1231
1232void ASTReader::Error(StringRef Msg) const {
1233 Error(diag::err_fe_pch_malformed, Msg);
1234 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1235 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1236 Diag(diag::note_module_cache_path)
1237 << PP.getHeaderSearchInfo().getModuleCachePath();
1238 }
1239}
1240
1241void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1242 StringRef Arg3) const {
1243 if (Diags.isDiagnosticInFlight())
1244 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1245 else
1246 Diag(DiagID) << Arg1 << Arg2 << Arg3;
1247}
1248
1249void ASTReader::Error(llvm::Error &&Err) const {
1250 Error(toString(std::move(Err)));
1251}
1252
1253//===----------------------------------------------------------------------===//
1254// Source Manager Deserialization
1255//===----------------------------------------------------------------------===//
1256
1257/// Read the line table in the source manager block.
1258/// \returns true if there was an error.
1259bool ASTReader::ParseLineTable(ModuleFile &F,
1260 const RecordData &Record) {
1261 unsigned Idx = 0;
1262 LineTableInfo &LineTable = SourceMgr.getLineTable();
1263
1264 // Parse the file names
1265 std::map<int, int> FileIDs;
1266 FileIDs[-1] = -1; // For unspecified filenames.
1267 for (unsigned I = 0; Record[Idx]; ++I) {
1268 // Extract the file name
1269 auto Filename = ReadPath(F, Record, Idx);
1270 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1271 }
1272 ++Idx;
1273
1274 // Parse the line entries
1275 std::vector<LineEntry> Entries;
1276 while (Idx < Record.size()) {
1277 int FID = Record[Idx++];
1278 assert(FID >= 0 && "Serialized line entries for non-local file.");
1279 // Remap FileID from 1-based old view.
1280 FID += F.SLocEntryBaseID - 1;
1281
1282 // Extract the line entries
1283 unsigned NumEntries = Record[Idx++];
1284 assert(NumEntries && "no line entries for file ID");
1285 Entries.clear();
1286 Entries.reserve(NumEntries);
1287 for (unsigned I = 0; I != NumEntries; ++I) {
1288 unsigned FileOffset = Record[Idx++];
1289 unsigned LineNo = Record[Idx++];
1290 int FilenameID = FileIDs[Record[Idx++]];
1291 SrcMgr::CharacteristicKind FileKind
1292 = (SrcMgr::CharacteristicKind)Record[Idx++];
1293 unsigned IncludeOffset = Record[Idx++];
1294 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1295 FileKind, IncludeOffset));
1296 }
1297 LineTable.AddEntry(FileID::get(FID), Entries);
1298 }
1299
1300 return false;
1301}
1302
1303/// Read a source manager block
1304bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1305 using namespace SrcMgr;
1306
1307 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1308
1309 // Set the source-location entry cursor to the current position in
1310 // the stream. This cursor will be used to read the contents of the
1311 // source manager block initially, and then lazily read
1312 // source-location entries as needed.
1313 SLocEntryCursor = F.Stream;
1314
1315 // The stream itself is going to skip over the source manager block.
1316 if (llvm::Error Err = F.Stream.SkipBlock()) {
1317 Error(std::move(Err));
1318 return true;
1319 }
1320
1321 // Enter the source manager block.
1322 if (llvm::Error Err =
1323 SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1324 Error(std::move(Err));
1325 return true;
1326 }
1327 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1328
1329 RecordData Record;
1330 while (true) {
1331 Expected<llvm::BitstreamEntry> MaybeE =
1332 SLocEntryCursor.advanceSkippingSubblocks();
1333 if (!MaybeE) {
1334 Error(MaybeE.takeError());
1335 return true;
1336 }
1337 llvm::BitstreamEntry E = MaybeE.get();
1338
1339 switch (E.Kind) {
1340 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1341 case llvm::BitstreamEntry::Error:
1342 Error("malformed block record in AST file");
1343 return true;
1344 case llvm::BitstreamEntry::EndBlock:
1345 return false;
1346 case llvm::BitstreamEntry::Record:
1347 // The interesting case.
1348 break;
1349 }
1350
1351 // Read a record.
1352 Record.clear();
1353 StringRef Blob;
1354 Expected<unsigned> MaybeRecord =
1355 SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1356 if (!MaybeRecord) {
1357 Error(MaybeRecord.takeError());
1358 return true;
1359 }
1360 switch (MaybeRecord.get()) {
1361 default: // Default behavior: ignore.
1362 break;
1363
1364 case SM_SLOC_FILE_ENTRY:
1365 case SM_SLOC_BUFFER_ENTRY:
1366 case SM_SLOC_EXPANSION_ENTRY:
1367 // Once we hit one of the source location entries, we're done.
1368 return false;
1369 }
1370 }
1371}
1372
1373/// If a header file is not found at the path that we expect it to be
1374/// and the PCH file was moved from its original location, try to resolve the
1375/// file by assuming that header+PCH were moved together and the header is in
1376/// the same place relative to the PCH.
1377static std::string
1378resolveFileRelativeToOriginalDir(const std::string &Filename,
1379 const std::string &OriginalDir,
1380 const std::string &CurrDir) {
1381 assert(OriginalDir != CurrDir &&
1382 "No point trying to resolve the file if the PCH dir didn't change");
1383
1384 using namespace llvm::sys;
1385
1386 SmallString<128> filePath(Filename);
1387 fs::make_absolute(filePath);
1388 assert(path::is_absolute(OriginalDir));
1389 SmallString<128> currPCHPath(CurrDir);
1390
1391 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1392 fileDirE = path::end(path::parent_path(filePath));
1393 path::const_iterator origDirI = path::begin(OriginalDir),
1394 origDirE = path::end(OriginalDir);
1395 // Skip the common path components from filePath and OriginalDir.
1396 while (fileDirI != fileDirE && origDirI != origDirE &&
1397 *fileDirI == *origDirI) {
1398 ++fileDirI;
1399 ++origDirI;
1400 }
1401 for (; origDirI != origDirE; ++origDirI)
1402 path::append(currPCHPath, "..");
1403 path::append(currPCHPath, fileDirI, fileDirE);
1404 path::append(currPCHPath, path::filename(Filename));
1405 return std::string(currPCHPath.str());
1406}
1407
1408bool ASTReader::ReadSLocEntry(int ID) {
1409 if (ID == 0)
1410 return false;
1411
1412 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1413 Error("source location entry ID out-of-range for AST file");
1414 return true;
1415 }
1416
1417 // Local helper to read the (possibly-compressed) buffer data following the
1418 // entry record.
1419 auto ReadBuffer = [this](
1420 BitstreamCursor &SLocEntryCursor,
1421 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1422 RecordData Record;
1423 StringRef Blob;
1424 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1425 if (!MaybeCode) {
1426 Error(MaybeCode.takeError());
1427 return nullptr;
1428 }
1429 unsigned Code = MaybeCode.get();
1430
1431 Expected<unsigned> MaybeRecCode =
1432 SLocEntryCursor.readRecord(Code, Record, &Blob);
1433 if (!MaybeRecCode) {
1434 Error(MaybeRecCode.takeError());
1435 return nullptr;
1436 }
1437 unsigned RecCode = MaybeRecCode.get();
1438
1439 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1440 if (!llvm::zlib::isAvailable()) {
1441 Error("zlib is not available");
1442 return nullptr;
1443 }
1444 SmallString<0> Uncompressed;
1445 if (llvm::Error E =
1446 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1447 Error("could not decompress embedded file contents: " +
1448 llvm::toString(std::move(E)));
1449 return nullptr;
1450 }
1451 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1452 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1453 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1454 } else {
1455 Error("AST record has invalid code");
1456 return nullptr;
1457 }
1458 };
1459
1460 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1461 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1462 F->SLocEntryOffsetsBase +
1463 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1464 Error(std::move(Err));
1465 return true;
1466 }
1467
1468 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1469 unsigned BaseOffset = F->SLocEntryBaseOffset;
1470
1471 ++NumSLocEntriesRead;
1472 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1473 if (!MaybeEntry) {
1474 Error(MaybeEntry.takeError());
1475 return true;
1476 }
1477 llvm::BitstreamEntry Entry = MaybeEntry.get();
1478
1479 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1480 Error("incorrectly-formatted source location entry in AST file");
1481 return true;
1482 }
1483
1484 RecordData Record;
1485 StringRef Blob;
1486 Expected<unsigned> MaybeSLOC =
1487 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1488 if (!MaybeSLOC) {
1489 Error(MaybeSLOC.takeError());
1490 return true;
1491 }
1492 switch (MaybeSLOC.get()) {
1493 default:
1494 Error("incorrectly-formatted source location entry in AST file");
1495 return true;
1496
1497 case SM_SLOC_FILE_ENTRY: {
1498 // We will detect whether a file changed and return 'Failure' for it, but
1499 // we will also try to fail gracefully by setting up the SLocEntry.
1500 unsigned InputID = Record[4];
1501 InputFile IF = getInputFile(*F, InputID);
1502 Optional<FileEntryRef> File = IF.getFile();
1503 bool OverriddenBuffer = IF.isOverridden();
1504
1505 // Note that we only check if a File was returned. If it was out-of-date
1506 // we have complained but we will continue creating a FileID to recover
1507 // gracefully.
1508 if (!File)
1509 return true;
1510
1511 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1512 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1513 // This is the module's main file.
1514 IncludeLoc = getImportLocation(F);
1515 }
1516 SrcMgr::CharacteristicKind
1517 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1518 FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID,
1519 BaseOffset + Record[0]);
1520 SrcMgr::FileInfo &FileInfo =
1521 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1522 FileInfo.NumCreatedFIDs = Record[5];
1523 if (Record[3])
1524 FileInfo.setHasLineDirectives();
1525
1526 unsigned NumFileDecls = Record[7];
1527 if (NumFileDecls && ContextObj) {
1528 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1529 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1530 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1531 NumFileDecls));
1532 }
1533
1534 const SrcMgr::ContentCache &ContentCache =
1535 SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter));
1536 if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1537 ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1538 !ContentCache.getBufferIfLoaded()) {
1539 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1540 if (!Buffer)
1541 return true;
1542 SourceMgr.overrideFileContents(*File, std::move(Buffer));
1543 }
1544
1545 break;
1546 }
1547
1548 case SM_SLOC_BUFFER_ENTRY: {
1549 const char *Name = Blob.data();
1550 unsigned Offset = Record[0];
1551 SrcMgr::CharacteristicKind
1552 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1553 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1554 if (IncludeLoc.isInvalid() && F->isModule()) {
1555 IncludeLoc = getImportLocation(F);
1556 }
1557
1558 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1559 if (!Buffer)
1560 return true;
1561 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1562 BaseOffset + Offset, IncludeLoc);
1563 break;
1564 }
1565
1566 case SM_SLOC_EXPANSION_ENTRY: {
1567 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1568 SourceMgr.createExpansionLoc(SpellingLoc,
1569 ReadSourceLocation(*F, Record[2]),
1570 ReadSourceLocation(*F, Record[3]),
1571 Record[5],
1572 Record[4],
1573 ID,
1574 BaseOffset + Record[0]);
1575 break;
1576 }
1577 }
1578
1579 return false;
1580}
1581
1582std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1583 if (ID == 0)
1584 return std::make_pair(SourceLocation(), "");
1585
1586 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1587 Error("source location entry ID out-of-range for AST file");
1588 return std::make_pair(SourceLocation(), "");
1589 }
1590
1591 // Find which module file this entry lands in.
1592 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1593 if (!M->isModule())
1594 return std::make_pair(SourceLocation(), "");
1595
1596 // FIXME: Can we map this down to a particular submodule? That would be
1597 // ideal.
1598 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1599}
1600
1601/// Find the location where the module F is imported.
1602SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1603 if (F->ImportLoc.isValid())
1604 return F->ImportLoc;
1605
1606 // Otherwise we have a PCH. It's considered to be "imported" at the first
1607 // location of its includer.
1608 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1609 // Main file is the importer.
1610 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1611 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1612 }
1613 return F->ImportedBy[0]->FirstLoc;
1614}
1615
1616/// Enter a subblock of the specified BlockID with the specified cursor. Read
1617/// the abbreviations that are at the top of the block and then leave the cursor
1618/// pointing into the block.
1619bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID,
1620 uint64_t *StartOfBlockOffset) {
1621 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1622 // FIXME this drops errors on the floor.
1623 consumeError(std::move(Err));
1624 return true;
1625 }
1626
1627 if (StartOfBlockOffset)
1628 *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1629
1630 while (true) {
1631 uint64_t Offset = Cursor.GetCurrentBitNo();
1632 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1633 if (!MaybeCode) {
1634 // FIXME this drops errors on the floor.
1635 consumeError(MaybeCode.takeError());
1636 return true;
1637 }
1638 unsigned Code = MaybeCode.get();
1639
1640 // We expect all abbrevs to be at the start of the block.
1641 if (Code != llvm::bitc::DEFINE_ABBREV) {
1642 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1643 // FIXME this drops errors on the floor.
1644 consumeError(std::move(Err));
1645 return true;
1646 }
1647 return false;
1648 }
1649 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1650 // FIXME this drops errors on the floor.
1651 consumeError(std::move(Err));
1652 return true;
1653 }
1654 }
1655}
1656
1657Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1658 unsigned &Idx) {
1659 Token Tok;
1660 Tok.startToken();
1661 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1662 Tok.setLength(Record[Idx++]);
1663 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1664 Tok.setIdentifierInfo(II);
1665 Tok.setKind((tok::TokenKind)Record[Idx++]);
1666 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1667 return Tok;
1668}
1669
1670MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1671 BitstreamCursor &Stream = F.MacroCursor;
1672
1673 // Keep track of where we are in the stream, then jump back there
1674 // after reading this macro.
1675 SavedStreamPosition SavedPosition(Stream);
1676
1677 if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1678 // FIXME this drops errors on the floor.
1679 consumeError(std::move(Err));
1680 return nullptr;
1681 }
1682 RecordData Record;
1683 SmallVector<IdentifierInfo*, 16> MacroParams;
1684 MacroInfo *Macro = nullptr;
1685
1686 while (true) {
1687 // Advance to the next record, but if we get to the end of the block, don't
1688 // pop it (removing all the abbreviations from the cursor) since we want to
1689 // be able to reseek within the block and read entries.
1690 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1691 Expected<llvm::BitstreamEntry> MaybeEntry =
1692 Stream.advanceSkippingSubblocks(Flags);
1693 if (!MaybeEntry) {
1694 Error(MaybeEntry.takeError());
1695 return Macro;
1696 }
1697 llvm::BitstreamEntry Entry = MaybeEntry.get();
1698
1699 switch (Entry.Kind) {
1700 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1701 case llvm::BitstreamEntry::Error:
1702 Error("malformed block record in AST file");
1703 return Macro;
1704 case llvm::BitstreamEntry::EndBlock:
1705 return Macro;
1706 case llvm::BitstreamEntry::Record:
1707 // The interesting case.
1708 break;
1709 }
1710
1711 // Read a record.
1712 Record.clear();
1713 PreprocessorRecordTypes RecType;
1714 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1715 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1716 else {
1717 Error(MaybeRecType.takeError());
1718 return Macro;
1719 }
1720 switch (RecType) {
1721 case PP_MODULE_MACRO:
1722 case PP_MACRO_DIRECTIVE_HISTORY:
1723 return Macro;
1724
1725 case PP_MACRO_OBJECT_LIKE:
1726 case PP_MACRO_FUNCTION_LIKE: {
1727 // If we already have a macro, that means that we've hit the end
1728 // of the definition of the macro we were looking for. We're
1729 // done.
1730 if (Macro)
1731 return Macro;
1732
1733 unsigned NextIndex = 1; // Skip identifier ID.
1734 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1735 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1736 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1737 MI->setIsUsed(Record[NextIndex++]);
1738 MI->setUsedForHeaderGuard(Record[NextIndex++]);
1739
1740 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1741 // Decode function-like macro info.
1742 bool isC99VarArgs = Record[NextIndex++];
1743 bool isGNUVarArgs = Record[NextIndex++];
1744 bool hasCommaPasting = Record[NextIndex++];
1745 MacroParams.clear();
1746 unsigned NumArgs = Record[NextIndex++];
1747 for (unsigned i = 0; i != NumArgs; ++i)
1748 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1749
1750 // Install function-like macro info.
1751 MI->setIsFunctionLike();
1752 if (isC99VarArgs) MI->setIsC99Varargs();
1753 if (isGNUVarArgs) MI->setIsGNUVarargs();
1754 if (hasCommaPasting) MI->setHasCommaPasting();
1755 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1756 }
1757
1758 // Remember that we saw this macro last so that we add the tokens that
1759 // form its body to it.
1760 Macro = MI;
1761
1762 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1763 Record[NextIndex]) {
1764 // We have a macro definition. Register the association
1765 PreprocessedEntityID
1766 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1767 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1768 PreprocessingRecord::PPEntityID PPID =
1769 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1770 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1771 PPRec.getPreprocessedEntity(PPID));
1772 if (PPDef)
1773 PPRec.RegisterMacroDefinition(Macro, PPDef);
1774 }
1775
1776 ++NumMacrosRead;
1777 break;
1778 }
1779
1780 case PP_TOKEN: {
1781 // If we see a TOKEN before a PP_MACRO_*, then the file is
1782 // erroneous, just pretend we didn't see this.
1783 if (!Macro) break;
1784
1785 unsigned Idx = 0;
1786 Token Tok = ReadToken(F, Record, Idx);
1787 Macro->AddTokenToBody(Tok);
1788 break;
1789 }
1790 }
1791 }
1792}
1793
1794PreprocessedEntityID
1795ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1796 unsigned LocalID) const {
1797 if (!M.ModuleOffsetMap.empty())
1798 ReadModuleOffsetMap(M);
1799
1800 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1801 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1802 assert(I != M.PreprocessedEntityRemap.end()
1803 && "Invalid index into preprocessed entity index remap");
1804
1805 return LocalID + I->second;
1806}
1807
1808unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1809 return llvm::hash_combine(ikey.Size, ikey.ModTime);
1810}
1811
1812HeaderFileInfoTrait::internal_key_type
1813HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1814 internal_key_type ikey = {FE->getSize(),
1815 M.HasTimestamps ? FE->getModificationTime() : 0,
1816 FE->getName(), /*Imported*/ false};
1817 return ikey;
1818}
1819
1820bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1821 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1822 return false;
1823
1824 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1825 return true;
1826
1827 // Determine whether the actual files are equivalent.
1828 FileManager &FileMgr = Reader.getFileManager();
1829 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1830 if (!Key.Imported) {
1831 if (auto File = FileMgr.getFile(Key.Filename))
1832 return *File;
1833 return nullptr;
1834 }
1835
1836 std::string Resolved = std::string(Key.Filename);
1837 Reader.ResolveImportedPath(M, Resolved);
1838 if (auto File = FileMgr.getFile(Resolved))
1839 return *File;
1840 return nullptr;
1841 };
1842
1843 const FileEntry *FEA = GetFile(a);
1844 const FileEntry *FEB = GetFile(b);
1845 return FEA && FEA == FEB;
1846}
1847
1848std::pair<unsigned, unsigned>
1849HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1850 using namespace llvm::support;
1851
1852 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1853 unsigned DataLen = (unsigned) *d++;
1854 return std::make_pair(KeyLen, DataLen);
1855}
1856
1857HeaderFileInfoTrait::internal_key_type
1858HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1859 using namespace llvm::support;
1860
1861 internal_key_type ikey;
1862 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1863 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1864 ikey.Filename = (const char *)d;
1865 ikey.Imported = true;
1866 return ikey;
1867}
1868
1869HeaderFileInfoTrait::data_type
1870HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1871 unsigned DataLen) {
1872 using namespace llvm::support;
1873
1874 const unsigned char *End = d + DataLen;
1875 HeaderFileInfo HFI;
1876 unsigned Flags = *d++;
1877 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1878 HFI.isImport |= (Flags >> 5) & 0x01;
1879 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1880 HFI.DirInfo = (Flags >> 1) & 0x07;
1881 HFI.IndexHeaderMapHeader = Flags & 0x01;
1882 // FIXME: Find a better way to handle this. Maybe just store a
1883 // "has been included" flag?
1884 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1885 HFI.NumIncludes);
1886 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1887 M, endian::readNext<uint32_t, little, unaligned>(d));
1888 if (unsigned FrameworkOffset =
1889 endian::readNext<uint32_t, little, unaligned>(d)) {
1890 // The framework offset is 1 greater than the actual offset,
1891 // since 0 is used as an indicator for "no framework name".
1892 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1893 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1894 }
1895
1896 assert((End - d) % 4 == 0 &&
1897 "Wrong data length in HeaderFileInfo deserialization");
1898 while (d != End) {
1899 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1900 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1901 LocalSMID >>= 2;
1902
1903 // This header is part of a module. Associate it with the module to enable
1904 // implicit module import.
1905 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1906 Module *Mod = Reader.getSubmodule(GlobalSMID);
1907 FileManager &FileMgr = Reader.getFileManager();
1908 ModuleMap &ModMap =
1909 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1910
1911 std::string Filename = std::string(key.Filename);
1912 if (key.Imported)
1913 Reader.ResolveImportedPath(M, Filename);
1914 // FIXME: This is not always the right filename-as-written, but we're not
1915 // going to use this information to rebuild the module, so it doesn't make
1916 // a lot of difference.
1917 Module::Header H = {std::string(key.Filename),
1918 *FileMgr.getOptionalFileRef(Filename)};
1919 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1920 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1921 }
1922
1923 // This HeaderFileInfo was externally loaded.
1924 HFI.External = true;
1925 HFI.IsValid = true;
1926 return HFI;
1927}
1928
1929void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1930 uint32_t MacroDirectivesOffset) {
1931 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1932 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1933}
1934
1935void ASTReader::ReadDefinedMacros() {
1936 // Note that we are loading defined macros.
1937 Deserializing Macros(this);
1938
1939 for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1940 BitstreamCursor &MacroCursor = I.MacroCursor;
1941
1942 // If there was no preprocessor block, skip this file.
1943 if (MacroCursor.getBitcodeBytes().empty())
1944 continue;
1945
1946 BitstreamCursor Cursor = MacroCursor;
1947 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1948 Error(std::move(Err));
1949 return;
1950 }
1951
1952 RecordData Record;
1953 while (true) {
1954 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1955 if (!MaybeE) {
1956 Error(MaybeE.takeError());
1957 return;
1958 }
1959 llvm::BitstreamEntry E = MaybeE.get();
1960
1961 switch (E.Kind) {
1962 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1963 case llvm::BitstreamEntry::Error:
1964 Error("malformed block record in AST file");
1965 return;
1966 case llvm::BitstreamEntry::EndBlock:
1967 goto NextCursor;
1968
1969 case llvm::BitstreamEntry::Record: {
1970 Record.clear();
1971 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1972 if (!MaybeRecord) {
1973 Error(MaybeRecord.takeError());
1974 return;
1975 }
1976 switch (MaybeRecord.get()) {
1977 default: // Default behavior: ignore.
1978 break;
1979
1980 case PP_MACRO_OBJECT_LIKE:
1981 case PP_MACRO_FUNCTION_LIKE: {
1982 IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1983 if (II->isOutOfDate())
1984 updateOutOfDateIdentifier(*II);
1985 break;
1986 }
1987
1988 case PP_TOKEN:
1989 // Ignore tokens.
1990 break;
1991 }
1992 break;
1993 }
1994 }
1995 }
1996 NextCursor: ;
1997 }
1998}
1999
2000namespace {
2001
2002 /// Visitor class used to look up identifirs in an AST file.
2003 class IdentifierLookupVisitor {
2004 StringRef Name;
2005 unsigned NameHash;
2006 unsigned PriorGeneration;
2007 unsigned &NumIdentifierLookups;
2008 unsigned &NumIdentifierLookupHits;
2009 IdentifierInfo *Found = nullptr;
2010
2011 public:
2012 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2013 unsigned &NumIdentifierLookups,
2014 unsigned &NumIdentifierLookupHits)
2015 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2016 PriorGeneration(PriorGeneration),
2017 NumIdentifierLookups(NumIdentifierLookups),
2018 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2019
2020 bool operator()(ModuleFile &M) {
2021 // If we've already searched this module file, skip it now.
2022 if (M.Generation <= PriorGeneration)
2023 return true;
2024
2025 ASTIdentifierLookupTable *IdTable
2026 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2027 if (!IdTable)
2028 return false;
2029
2030 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2031 Found);
2032 ++NumIdentifierLookups;
2033 ASTIdentifierLookupTable::iterator Pos =
2034 IdTable->find_hashed(Name, NameHash, &Trait);
2035 if (Pos == IdTable->end())
2036 return false;
2037
2038 // Dereferencing the iterator has the effect of building the
2039 // IdentifierInfo node and populating it with the various
2040 // declarations it needs.
2041 ++NumIdentifierLookupHits;
2042 Found = *Pos;
2043 return true;
2044 }
2045
2046 // Retrieve the identifier info found within the module
2047 // files.
2048 IdentifierInfo *getIdentifierInfo() const { return Found; }
2049 };
2050
2051} // namespace
2052
2053void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2054 // Note that we are loading an identifier.
2055 Deserializing AnIdentifier(this);
2056
2057 unsigned PriorGeneration = 0;
2058 if (getContext().getLangOpts().Modules)
2059 PriorGeneration = IdentifierGeneration[&II];
2060
2061 // If there is a global index, look there first to determine which modules
2062 // provably do not have any results for this identifier.
2063 GlobalModuleIndex::HitSet Hits;
2064 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2065 if (!loadGlobalIndex()) {
2066 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2067 HitsPtr = &Hits;
2068 }
2069 }
2070
2071 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2072 NumIdentifierLookups,
2073 NumIdentifierLookupHits);
2074 ModuleMgr.visit(Visitor, HitsPtr);
2075 markIdentifierUpToDate(&II);
2076}
2077
2078void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2079 if (!II)
2080 return;
2081
2082 II->setOutOfDate(false);
2083
2084 // Update the generation for this identifier.
2085 if (getContext().getLangOpts().Modules)
2086 IdentifierGeneration[II] = getGeneration();
2087}
2088
2089void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2090 const PendingMacroInfo &PMInfo) {
2091 ModuleFile &M = *PMInfo.M;
2092
2093 BitstreamCursor &Cursor = M.MacroCursor;
2094 SavedStreamPosition SavedPosition(Cursor);
2095 if (llvm::Error Err =
2096 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2097 Error(std::move(Err));
2098 return;
2099 }
2100
2101 struct ModuleMacroRecord {
2102 SubmoduleID SubModID;
2103 MacroInfo *MI;
2104 SmallVector<SubmoduleID, 8> Overrides;
2105 };
2106 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2107
2108 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2109 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2110 // macro histroy.
2111 RecordData Record;
2112 while (true) {
2113 Expected<llvm::BitstreamEntry> MaybeEntry =
2114 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2115 if (!MaybeEntry) {
2116 Error(MaybeEntry.takeError());
2117 return;
2118 }
2119 llvm::BitstreamEntry Entry = MaybeEntry.get();
2120
2121 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2122 Error("malformed block record in AST file");
2123 return;
2124 }
2125
2126 Record.clear();
2127 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2128 if (!MaybePP) {
2129 Error(MaybePP.takeError());
2130 return;
2131 }
2132 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2133 case PP_MACRO_DIRECTIVE_HISTORY:
2134 break;
2135
2136 case PP_MODULE_MACRO: {
2137 ModuleMacros.push_back(ModuleMacroRecord());
2138 auto &Info = ModuleMacros.back();
2139 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2140 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2141 for (int I = 2, N = Record.size(); I != N; ++I)
2142 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2143 continue;
2144 }
2145
2146 default:
2147 Error("malformed block record in AST file");
2148 return;
2149 }
2150
2151 // We found the macro directive history; that's the last record
2152 // for this macro.
2153 break;
2154 }
2155
2156 // Module macros are listed in reverse dependency order.
2157 {
2158 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2159 llvm::SmallVector<ModuleMacro*, 8> Overrides;
2160 for (auto &MMR : ModuleMacros) {
2161 Overrides.clear();
2162 for (unsigned ModID : MMR.Overrides) {
2163 Module *Mod = getSubmodule(ModID);
2164 auto *Macro = PP.getModuleMacro(Mod, II);
2165 assert(Macro && "missing definition for overridden macro");
2166 Overrides.push_back(Macro);
2167 }
2168
2169 bool Inserted = false;
2170 Module *Owner = getSubmodule(MMR.SubModID);
2171 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2172 }
2173 }
2174
2175 // Don't read the directive history for a module; we don't have anywhere
2176 // to put it.
2177 if (M.isModule())
2178 return;
2179
2180 // Deserialize the macro directives history in reverse source-order.
2181 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2182 unsigned Idx = 0, N = Record.size();
2183 while (Idx < N) {
2184 MacroDirective *MD = nullptr;
2185 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2186 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2187 switch (K) {
2188 case MacroDirective::MD_Define: {
2189 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2190 MD = PP.AllocateDefMacroDirective(MI, Loc);
2191 break;
2192 }
2193 case MacroDirective::MD_Undefine:
2194 MD = PP.AllocateUndefMacroDirective(Loc);
2195 break;
2196 case MacroDirective::MD_Visibility:
2197 bool isPublic = Record[Idx++];
2198 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2199 break;
2200 }
2201
2202 if (!Latest)
2203 Latest = MD;
2204 if (Earliest)
2205 Earliest->setPrevious(MD);
2206 Earliest = MD;
2207 }
2208
2209 if (Latest)
2210 PP.setLoadedMacroDirective(II, Earliest, Latest);
2211}
2212
2213bool ASTReader::shouldDisableValidationForFile(
2214 const serialization::ModuleFile &M) const {
2215 if (DisableValidationKind == DisableValidationForModuleKind::None)
2216 return false;
2217
2218 // If a PCH is loaded and validation is disabled for PCH then disable
2219 // validation for the PCH and the modules it loads.
2220 ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind);
2221
2222 switch (K) {
2223 case MK_MainFile:
2224 case MK_Preamble:
2225 case MK_PCH:
2226 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2227 case MK_ImplicitModule:
2228 case MK_ExplicitModule:
2229 case MK_PrebuiltModule:
2230 return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2231 }
2232
2233 return false;
2234}
2235
2236ASTReader::InputFileInfo
2237ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2238 // Go find this input file.
2239 BitstreamCursor &Cursor = F.InputFilesCursor;
2240 SavedStreamPosition SavedPosition(Cursor);
2241 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2242 // FIXME this drops errors on the floor.
2243 consumeError(std::move(Err));
2244 }
2245
2246 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2247 if (!MaybeCode) {
2248 // FIXME this drops errors on the floor.
2249 consumeError(MaybeCode.takeError());
2250 }
2251 unsigned Code = MaybeCode.get();
2252 RecordData Record;
2253 StringRef Blob;
2254
2255 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2256 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2257 "invalid record type for input file");
2258 else {
2259 // FIXME this drops errors on the floor.
2260 consumeError(Maybe.takeError());
2261 }
2262
2263 assert(Record[0] == ID && "Bogus stored ID or offset");
2264 InputFileInfo R;
2265 R.StoredSize = static_cast<off_t>(Record[1]);
2266 R.StoredTime = static_cast<time_t>(Record[2]);
2267 R.Overridden = static_cast<bool>(Record[3]);
2268 R.Transient = static_cast<bool>(Record[4]);
2269 R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2270 R.Filename = std::string(Blob);
2271 ResolveImportedPath(F, R.Filename);
2272
2273 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2274 if (!MaybeEntry) // FIXME this drops errors on the floor.
2275 consumeError(MaybeEntry.takeError());
2276 llvm::BitstreamEntry Entry = MaybeEntry.get();
2277 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2278 "expected record type for input file hash");
2279
2280 Record.clear();
2281 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2282 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2283 "invalid record type for input file hash");
2284 else {
2285 // FIXME this drops errors on the floor.
2286 consumeError(Maybe.takeError());
2287 }
2288 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2289 static_cast<uint64_t>(Record[0]);
2290 return R;
2291}
2292
2293static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2294InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2295 // If this ID is bogus, just return an empty input file.
2296 if (ID == 0 || ID > F.InputFilesLoaded.size())
2297 return InputFile();
2298
2299 // If we've already loaded this input file, return it.
2300 if (F.InputFilesLoaded[ID-1].getFile())
2301 return F.InputFilesLoaded[ID-1];
2302
2303 if (F.InputFilesLoaded[ID-1].isNotFound())
2304 return InputFile();
2305
2306 // Go find this input file.
2307 BitstreamCursor &Cursor = F.InputFilesCursor;
2308 SavedStreamPosition SavedPosition(Cursor);
2309 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2310 // FIXME this drops errors on the floor.
2311 consumeError(std::move(Err));
2312 }
2313
2314 InputFileInfo FI = readInputFileInfo(F, ID);
2315 off_t StoredSize = FI.StoredSize;
2316 time_t StoredTime = FI.StoredTime;
2317 bool Overridden = FI.Overridden;
2318 bool Transient = FI.Transient;
2319 StringRef Filename = FI.Filename;
2320 uint64_t StoredContentHash = FI.ContentHash;
2321
2322 OptionalFileEntryRefDegradesToFileEntryPtr File =
2323 expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false));
2324
2325 // If we didn't find the file, resolve it relative to the
2326 // original directory from which this AST file was created.
2327 if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2328 F.OriginalDir != F.BaseDirectory) {
2329 std::string Resolved = resolveFileRelativeToOriginalDir(
2330 std::string(Filename), F.OriginalDir, F.BaseDirectory);
2331 if (!Resolved.empty())
2332 File = expectedToOptional(FileMgr.getFileRef(Resolved));
2333 }
2334
2335 // For an overridden file, create a virtual file with the stored
2336 // size/timestamp.
2337 if ((Overridden || Transient) && !File)
2338 File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime);
2339
2340 if (!File) {
2341 if (Complain) {
2342 std::string ErrorStr = "could not find file '";
2343 ErrorStr += Filename;
2344 ErrorStr += "' referenced by AST file '";
2345 ErrorStr += F.FileName;
2346 ErrorStr += "'";
2347 Error(ErrorStr);
2348 }
2349 // Record that we didn't find the file.
2350 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2351 return InputFile();
2352 }
2353
2354 // Check if there was a request to override the contents of the file
2355 // that was part of the precompiled header. Overriding such a file
2356 // can lead to problems when lexing using the source locations from the
2357 // PCH.
2358 SourceManager &SM = getSourceManager();
2359 // FIXME: Reject if the overrides are different.
2360 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2361 if (Complain)
2362 Error(diag::err_fe_pch_file_overridden, Filename);
2363
2364 // After emitting the diagnostic, bypass the overriding file to recover
2365 // (this creates a separate FileEntry).
2366 File = SM.bypassFileContentsOverride(*File);
2367 if (!File) {
2368 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2369 return InputFile();
2370 }
2371 }
2372
2373 enum ModificationType {
2374 Size,
2375 ModTime,
2376 Content,
2377 None,
2378 };
2379 auto HasInputFileChanged = [&]() {
2380 if (StoredSize != File->getSize())
2381 return ModificationType::Size;
2382 if (!shouldDisableValidationForFile(F) && StoredTime &&
2383 StoredTime != File->getModificationTime()) {
2384 // In case the modification time changes but not the content,
2385 // accept the cached file as legit.
2386 if (ValidateASTInputFilesContent &&
2387 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2388 auto MemBuffOrError = FileMgr.getBufferForFile(File);
2389 if (!MemBuffOrError) {
2390 if (!Complain)
2391 return ModificationType::ModTime;
2392 std::string ErrorStr = "could not get buffer for file '";
2393 ErrorStr += File->getName();
2394 ErrorStr += "'";
2395 Error(ErrorStr);
2396 return ModificationType::ModTime;
2397 }
2398
2399 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2400 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2401 return ModificationType::None;
2402 return ModificationType::Content;
2403 }
2404 return ModificationType::ModTime;
2405 }
2406 return ModificationType::None;
2407 };
2408
2409 bool IsOutOfDate = false;
2410 auto FileChange = HasInputFileChanged();
2411 // For an overridden file, there is nothing to validate.
2412 if (!Overridden && FileChange != ModificationType::None) {
2413 if (Complain && !Diags.isDiagnosticInFlight()) {
2414 // Build a list of the PCH imports that got us here (in reverse).
2415 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2416 while (!ImportStack.back()->ImportedBy.empty())
2417 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2418
2419 // The top-level PCH is stale.
2420 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2421 Diag(diag::err_fe_ast_file_modified)
2422 << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2423 << TopLevelPCHName << FileChange;
2424
2425 // Print the import stack.
2426 if (ImportStack.size() > 1) {
2427 Diag(diag::note_pch_required_by)
2428 << Filename << ImportStack[0]->FileName;
2429 for (unsigned I = 1; I < ImportStack.size(); ++I)
2430 Diag(diag::note_pch_required_by)
2431 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2432 }
2433
2434 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2435 }
2436
2437 IsOutOfDate = true;
2438 }
2439 // FIXME: If the file is overridden and we've already opened it,
2440 // issue an error (or split it into a separate FileEntry).
2441
2442 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2443
2444 // Note that we've loaded this input file.
2445 F.InputFilesLoaded[ID-1] = IF;
2446 return IF;
2447}
2448
2449/// If we are loading a relocatable PCH or module file, and the filename
2450/// is not an absolute path, add the system or module root to the beginning of
2451/// the file name.
2452void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2453 // Resolve relative to the base directory, if we have one.
2454 if (!M.BaseDirectory.empty())
2455 return ResolveImportedPath(Filename, M.BaseDirectory);
2456}
2457
2458void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2459 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2460 return;
2461
2462 SmallString<128> Buffer;
2463 llvm::sys::path::append(Buffer, Prefix, Filename);
2464 Filename.assign(Buffer.begin(), Buffer.end());
2465}
2466
2467static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2468 switch (ARR) {
2469 case ASTReader::Failure: return true;
2470 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2471 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2472 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2473 case ASTReader::ConfigurationMismatch:
2474 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2475 case ASTReader::HadErrors: return true;
2476 case ASTReader::Success: return false;
2477 }
2478
2479 llvm_unreachable("unknown ASTReadResult");
2480}
2481
2482ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2483 BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2484 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2485 std::string &SuggestedPredefines) {
2486 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2487 // FIXME this drops errors on the floor.
2488 consumeError(std::move(Err));
2489 return Failure;
2490 }
2491
2492 // Read all of the records in the options block.
2493 RecordData Record;
2494 ASTReadResult Result = Success;
2495 while (true) {
2496 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2497 if (!MaybeEntry) {
2498 // FIXME this drops errors on the floor.
2499 consumeError(MaybeEntry.takeError());
2500 return Failure;
2501 }
2502 llvm::BitstreamEntry Entry = MaybeEntry.get();
2503
2504 switch (Entry.Kind) {
2505 case llvm::BitstreamEntry::Error:
2506 case llvm::BitstreamEntry::SubBlock:
2507 return Failure;
2508
2509 case llvm::BitstreamEntry::EndBlock:
2510 return Result;
2511
2512 case llvm::BitstreamEntry::Record:
2513 // The interesting case.
2514 break;
2515 }
2516
2517 // Read and process a record.
2518 Record.clear();
2519 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2520 if (!MaybeRecordType) {
2521 // FIXME this drops errors on the floor.
2522 consumeError(MaybeRecordType.takeError());
2523 return Failure;
2524 }
2525 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2526 case LANGUAGE_OPTIONS: {
2527 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2528 if (ParseLanguageOptions(Record, Complain, Listener,
2529 AllowCompatibleConfigurationMismatch))
2530 Result = ConfigurationMismatch;
2531 break;
2532 }
2533
2534 case TARGET_OPTIONS: {
2535 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2536 if (ParseTargetOptions(Record, Complain, Listener,
2537 AllowCompatibleConfigurationMismatch))
2538 Result = ConfigurationMismatch;
2539 break;
2540 }
2541
2542 case FILE_SYSTEM_OPTIONS: {
2543 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2544 if (!AllowCompatibleConfigurationMismatch &&
2545 ParseFileSystemOptions(Record, Complain, Listener))
2546 Result = ConfigurationMismatch;
2547 break;
2548 }
2549
2550 case HEADER_SEARCH_OPTIONS: {
2551 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2552 if (!AllowCompatibleConfigurationMismatch &&
2553 ParseHeaderSearchOptions(Record, Complain, Listener))
2554 Result = ConfigurationMismatch;
2555 break;
2556 }
2557
2558 case PREPROCESSOR_OPTIONS:
2559 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2560 if (!AllowCompatibleConfigurationMismatch &&
2561 ParsePreprocessorOptions(Record, Complain, Listener,
2562 SuggestedPredefines))
2563 Result = ConfigurationMismatch;
2564 break;
2565 }
2566 }
2567}
2568
2569ASTReader::ASTReadResult
2570ASTReader::ReadControlBlock(ModuleFile &F,
2571 SmallVectorImpl<ImportedModule> &Loaded,
2572 const ModuleFile *ImportedBy,
2573 unsigned ClientLoadCapabilities) {
2574 BitstreamCursor &Stream = F.Stream;
2575
2576 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2577 Error(std::move(Err));
2578 return Failure;
2579 }
2580
2581 // Lambda to read the unhashed control block the first time it's called.
2582 //
2583 // For PCM files, the unhashed control block cannot be read until after the
2584 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
2585 // need to look ahead before reading the IMPORTS record. For consistency,
2586 // this block is always read somehow (see BitstreamEntry::EndBlock).
2587 bool HasReadUnhashedControlBlock = false;
2588 auto readUnhashedControlBlockOnce = [&]() {
2589 if (!HasReadUnhashedControlBlock) {
2590 HasReadUnhashedControlBlock = true;
2591 if (ASTReadResult Result =
2592 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2593 return Result;
2594 }
2595 return Success;
2596 };
2597
2598 bool DisableValidation = shouldDisableValidationForFile(F);
2599
2600 // Read all of the records and blocks in the control block.
2601 RecordData Record;
2602 unsigned NumInputs = 0;
2603 unsigned NumUserInputs = 0;
2604 StringRef BaseDirectoryAsWritten;
2605 while (true) {
2606 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2607 if (!MaybeEntry) {
2608 Error(MaybeEntry.takeError());
2609 return Failure;
2610 }
2611 llvm::BitstreamEntry Entry = MaybeEntry.get();
2612
2613 switch (Entry.Kind) {
2614 case llvm::BitstreamEntry::Error:
2615 Error("malformed block record in AST file");
2616 return Failure;
2617 case llvm::BitstreamEntry::EndBlock: {
2618 // Validate the module before returning. This call catches an AST with
2619 // no module name and no imports.
2620 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2621 return Result;
2622
2623 // Validate input files.
2624 const HeaderSearchOptions &HSOpts =
2625 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2626
2627 // All user input files reside at the index range [0, NumUserInputs), and
2628 // system input files reside at [NumUserInputs, NumInputs). For explicitly
2629 // loaded module files, ignore missing inputs.
2630 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2631 F.Kind != MK_PrebuiltModule) {
2632 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2633
2634 // If we are reading a module, we will create a verification timestamp,
2635 // so we verify all input files. Otherwise, verify only user input
2636 // files.
2637
2638 unsigned N = NumUserInputs;
2639 if (ValidateSystemInputs ||
2640 (HSOpts.ModulesValidateOncePerBuildSession &&
2641 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2642 F.Kind == MK_ImplicitModule))
2643 N = NumInputs;
2644
2645 for (unsigned I = 0; I < N; ++I) {
2646 InputFile IF = getInputFile(F, I+1, Complain);
2647 if (!IF.getFile() || IF.isOutOfDate())
2648 return OutOfDate;
2649 }
2650 }
2651
2652 if (Listener)
2653 Listener->visitModuleFile(F.FileName, F.Kind);
2654
2655 if (Listener && Listener->needsInputFileVisitation()) {
2656 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2657 : NumUserInputs;
2658 for (unsigned I = 0; I < N; ++I) {
2659 bool IsSystem = I >= NumUserInputs;
2660 InputFileInfo FI = readInputFileInfo(F, I+1);
2661 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2662 F.Kind == MK_ExplicitModule ||
2663 F.Kind == MK_PrebuiltModule);
2664 }
2665 }
2666
2667 return Success;
2668 }
2669
2670 case llvm::BitstreamEntry::SubBlock:
2671 switch (Entry.ID) {
2672 case INPUT_FILES_BLOCK_ID:
2673 F.InputFilesCursor = Stream;
2674 if (llvm::Error Err = Stream.SkipBlock()) {
2675 Error(std::move(Err));
2676 return Failure;
2677 }
2678 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2679 Error("malformed block record in AST file");
2680 return Failure;
2681 }
2682 continue;
2683
2684 case OPTIONS_BLOCK_ID:
2685 // If we're reading the first module for this group, check its options
2686 // are compatible with ours. For modules it imports, no further checking
2687 // is required, because we checked them when we built it.
2688 if (Listener && !ImportedBy) {
2689 // Should we allow the configuration of the module file to differ from
2690 // the configuration of the current translation unit in a compatible
2691 // way?
2692 //
2693 // FIXME: Allow this for files explicitly specified with -include-pch.
2694 bool AllowCompatibleConfigurationMismatch =
2695 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2696
2697 ASTReadResult Result =
2698 ReadOptionsBlock(Stream, ClientLoadCapabilities,
2699 AllowCompatibleConfigurationMismatch, *Listener,
2700 SuggestedPredefines);
2701 if (Result == Failure) {
2702 Error("malformed block record in AST file");
2703 return Result;
2704 }
2705
2706 if (DisableValidation ||
2707 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2708 Result = Success;
2709
2710 // If we can't load the module, exit early since we likely
2711 // will rebuild the module anyway. The stream may be in the
2712 // middle of a block.
2713 if (Result != Success)
2714 return Result;
2715 } else if (llvm::Error Err = Stream.SkipBlock()) {
2716 Error(std::move(Err));
2717 return Failure;
2718 }
2719 continue;
2720
2721 default:
2722 if (llvm::Error Err = Stream.SkipBlock()) {
2723 Error(std::move(Err));
2724 return Failure;
2725 }
2726 continue;
2727 }
2728
2729 case llvm::BitstreamEntry::Record:
2730 // The interesting case.
2731 break;
2732 }
2733
2734 // Read and process a record.
2735 Record.clear();
2736 StringRef Blob;
2737 Expected<unsigned> MaybeRecordType =
2738 Stream.readRecord(Entry.ID, Record, &Blob);
2739 if (!MaybeRecordType) {
2740 Error(MaybeRecordType.takeError());
2741 return Failure;
2742 }
2743 switch ((ControlRecordTypes)MaybeRecordType.get()) {
2744 case METADATA: {
2745 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2746 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2747 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2748 : diag::err_pch_version_too_new);
2749 return VersionMismatch;
2750 }
2751
2752 bool hasErrors = Record[6];
2753 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2754 Diag(diag::err_pch_with_compiler_errors);
2755 return HadErrors;
2756 }
2757 if (hasErrors) {
2758 Diags.ErrorOccurred = true;
2759 Diags.UncompilableErrorOccurred = true;
2760 Diags.UnrecoverableErrorOccurred = true;
2761 }
2762
2763 F.RelocatablePCH = Record[4];
2764 // Relative paths in a relocatable PCH are relative to our sysroot.
2765 if (F.RelocatablePCH)
2766 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2767
2768 F.HasTimestamps = Record[5];
2769
2770 const std::string &CurBranch = getClangFullRepositoryVersion();
2771 StringRef ASTBranch = Blob;
2772 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2773 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2774 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2775 return VersionMismatch;
2776 }
2777 break;
2778 }
2779
2780 case IMPORTS: {
2781 // Validate the AST before processing any imports (otherwise, untangling
2782 // them can be error-prone and expensive). A module will have a name and
2783 // will already have been validated, but this catches the PCH case.
2784 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2785 return Result;
2786
2787 // Load each of the imported PCH files.
2788 unsigned Idx = 0, N = Record.size();
2789 while (Idx < N) {
2790 // Read information about the AST file.
2791 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2792 // The import location will be the local one for now; we will adjust
2793 // all import locations of module imports after the global source
2794 // location info are setup, in ReadAST.
2795 SourceLocation ImportLoc =
2796 ReadUntranslatedSourceLocation(Record[Idx++]);
2797 off_t StoredSize = (off_t)Record[Idx++];
2798 time_t StoredModTime = (time_t)Record[Idx++];
2799 auto FirstSignatureByte = Record.begin() + Idx;
2800 ASTFileSignature StoredSignature = ASTFileSignature::create(
2801 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2802 Idx += ASTFileSignature::size;
2803
2804 std::string ImportedName = ReadString(Record, Idx);
2805 std::string ImportedFile;
2806
2807 // For prebuilt and explicit modules first consult the file map for
2808 // an override. Note that here we don't search prebuilt module
2809 // directories, only the explicit name to file mappings. Also, we will
2810 // still verify the size/signature making sure it is essentially the
2811 // same file but perhaps in a different location.
2812 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2813 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2814 ImportedName, /*FileMapOnly*/ true);
2815
2816 if (ImportedFile.empty())
2817 // Use BaseDirectoryAsWritten to ensure we use the same path in the
2818 // ModuleCache as when writing.
2819 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2820 else
2821 SkipPath(Record, Idx);
2822
2823 // If our client can't cope with us being out of date, we can't cope with
2824 // our dependency being missing.
2825 unsigned Capabilities = ClientLoadCapabilities;
2826 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2827 Capabilities &= ~ARR_Missing;
2828
2829 // Load the AST file.
2830 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2831 Loaded, StoredSize, StoredModTime,
2832 StoredSignature, Capabilities);
2833
2834 // If we diagnosed a problem, produce a backtrace.
2835 if (isDiagnosedResult(Result, Capabilities))
2836 Diag(diag::note_module_file_imported_by)
2837 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2838
2839 switch (Result) {
2840 case Failure: return Failure;
2841 // If we have to ignore the dependency, we'll have to ignore this too.
2842 case Missing:
2843 case OutOfDate: return OutOfDate;
2844 case VersionMismatch: return VersionMismatch;
2845 case ConfigurationMismatch: return ConfigurationMismatch;
2846 case HadErrors: return HadErrors;
2847 case Success: break;
2848 }
2849 }
2850 break;
2851 }
2852
2853 case ORIGINAL_FILE:
2854 F.OriginalSourceFileID = FileID::get(Record[0]);
2855 F.ActualOriginalSourceFileName = std::string(Blob);
2856 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2857 ResolveImportedPath(F, F.OriginalSourceFileName);
2858 break;
2859
2860 case ORIGINAL_FILE_ID:
2861 F.OriginalSourceFileID = FileID::get(Record[0]);
2862 break;
2863
2864 case ORIGINAL_PCH_DIR:
2865 F.OriginalDir = std::string(Blob);
2866 break;
2867
2868 case MODULE_NAME:
2869 F.ModuleName = std::string(Blob);
2870 Diag(diag::remark_module_import)
2871 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2872 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2873 if (Listener)
2874 Listener->ReadModuleName(F.ModuleName);
2875
2876 // Validate the AST as soon as we have a name so we can exit early on
2877 // failure.
2878 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2879 return Result;
2880
2881 break;
2882
2883 case MODULE_DIRECTORY: {
2884 // Save the BaseDirectory as written in the PCM for computing the module
2885 // filename for the ModuleCache.
2886 BaseDirectoryAsWritten = Blob;
2887 assert(!F.ModuleName.empty() &&
2888 "MODULE_DIRECTORY found before MODULE_NAME");
2889 // If we've already loaded a module map file covering this module, we may
2890 // have a better path for it (relative to the current build).
2891 Module *M = PP.getHeaderSearchInfo().lookupModule(
2892 F.ModuleName, /*AllowSearch*/ true,
2893 /*AllowExtraModuleMapSearch*/ true);
2894 if (M && M->Directory) {
2895 // If we're implicitly loading a module, the base directory can't
2896 // change between the build and use.
2897 // Don't emit module relocation error if we have -fno-validate-pch
2898 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
2899 DisableValidationForModuleKind::Module) &&
2900 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2901 auto BuildDir = PP.getFileManager().getDirectory(Blob);
2902 if (!BuildDir || *BuildDir != M->Directory) {
2903 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2904 Diag(diag::err_imported_module_relocated)
2905 << F.ModuleName << Blob << M->Directory->getName();
2906 return OutOfDate;
2907 }
2908 }
2909 F.BaseDirectory = std::string(M->Directory->getName());
2910 } else {
2911 F.BaseDirectory = std::string(Blob);
2912 }
2913 break;
2914 }
2915
2916 case MODULE_MAP_FILE:
2917 if (ASTReadResult Result =
2918 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2919 return Result;
2920 break;
2921
2922 case INPUT_FILE_OFFSETS:
2923 NumInputs = Record[0];
2924 NumUserInputs = Record[1];
2925 F.InputFileOffsets =
2926 (const llvm::support::unaligned_uint64_t *)Blob.data();
2927 F.InputFilesLoaded.resize(NumInputs);
2928 F.NumUserInputFiles = NumUserInputs;
2929 break;
2930 }
2931 }
2932}
2933
2934ASTReader::ASTReadResult
2935ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2936 BitstreamCursor &Stream = F.Stream;
2937
2938 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2939 Error(std::move(Err));
2940 return Failure;
2941 }
2942 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2943
2944 // Read all of the records and blocks for the AST file.
2945 RecordData Record;
2946 while (true) {
2947 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2948 if (!MaybeEntry) {
2949 Error(MaybeEntry.takeError());
2950 return Failure;
2951 }
2952 llvm::BitstreamEntry Entry = MaybeEntry.get();
2953
2954 switch (Entry.Kind) {
2955 case llvm::BitstreamEntry::Error:
2956 Error("error at end of module block in AST file");
2957 return Failure;
2958 case llvm::BitstreamEntry::EndBlock:
2959 // Outside of C++, we do not store a lookup map for the translation unit.
2960 // Instead, mark it as needing a lookup map to be built if this module
2961 // contains any declarations lexically within it (which it always does!).
2962 // This usually has no cost, since we very rarely need the lookup map for
2963 // the translation unit outside C++.
2964 if (ASTContext *Ctx = ContextObj) {
2965 DeclContext *DC = Ctx->getTranslationUnitDecl();
2966 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2967 DC->setMustBuildLookupTable();
2968 }
2969
2970 return Success;
2971 case llvm::BitstreamEntry::SubBlock:
2972 switch (Entry.ID) {
2973 case DECLTYPES_BLOCK_ID:
2974 // We lazily load the decls block, but we want to set up the
2975 // DeclsCursor cursor to point into it. Clone our current bitcode
2976 // cursor to it, enter the block and read the abbrevs in that block.
2977 // With the main cursor, we just skip over it.
2978 F.DeclsCursor = Stream;
2979 if (llvm::Error Err = Stream.SkipBlock()) {
2980 Error(std::move(Err));
2981 return Failure;
2982 }
2983 if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID,
2984 &F.DeclsBlockStartOffset)) {
2985 Error("malformed block record in AST file");
2986 return Failure;
2987 }
2988 break;
2989
2990 case PREPROCESSOR_BLOCK_ID:
2991 F.MacroCursor = Stream;
2992 if (!PP.getExternalSource())
2993 PP.setExternalSource(this);
2994
2995 if (llvm::Error Err = Stream.SkipBlock()) {
2996 Error(std::move(Err));
2997 return Failure;
2998 }
2999 if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
3000 Error("malformed block record in AST file");
3001 return Failure;
3002 }
3003 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3004 break;
3005
3006 case PREPROCESSOR_DETAIL_BLOCK_ID:
3007 F.PreprocessorDetailCursor = Stream;
3008
3009 if (llvm::Error Err = Stream.SkipBlock()) {
3010 Error(std::move(Err));
3011 return Failure;
3012 }
3013 if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3014 PREPROCESSOR_DETAIL_BLOCK_ID)) {
3015 Error("malformed preprocessor detail record in AST file");
3016 return Failure;
3017 }
3018 F.PreprocessorDetailStartOffset
3019 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3020
3021 if (!PP.getPreprocessingRecord())
3022 PP.createPreprocessingRecord();
3023 if (!PP.getPreprocessingRecord()->getExternalSource())
3024 PP.getPreprocessingRecord()->SetExternalSource(*this);
3025 break;
3026
3027 case SOURCE_MANAGER_BLOCK_ID:
3028 if (ReadSourceManagerBlock(F))
3029 return Failure;
3030 break;
3031
3032 case SUBMODULE_BLOCK_ID:
3033 if (ASTReadResult Result =
3034 ReadSubmoduleBlock(F, ClientLoadCapabilities))
3035 return Result;
3036 break;
3037
3038 case COMMENTS_BLOCK_ID: {
3039 BitstreamCursor C = Stream;
3040
3041 if (llvm::Error Err = Stream.SkipBlock()) {
3042 Error(std::move(Err));
3043 return Failure;
3044 }
3045 if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
3046 Error("malformed comments block in AST file");
3047 return Failure;
3048 }
3049 CommentsCursors.push_back(std::make_pair(C, &F));
3050 break;
3051 }
3052
3053 default:
3054 if (llvm::Error Err = Stream.SkipBlock()) {
3055 Error(std::move(Err));
3056 return Failure;
3057 }
3058 break;
3059 }
3060 continue;
3061
3062 case llvm::BitstreamEntry::Record:
3063 // The interesting case.
3064 break;
3065 }
3066
3067 // Read and process a record.
3068 Record.clear();
3069 StringRef Blob;
3070 Expected<unsigned> MaybeRecordType =
3071 Stream.readRecord(Entry.ID, Record, &Blob);
3072 if (!MaybeRecordType) {
3073 Error(MaybeRecordType.takeError());
3074 return Failure;
3075 }
3076 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3077
3078 // If we're not loading an AST context, we don't care about most records.
3079 if (!ContextObj) {
3080 switch (RecordType) {
3081 case IDENTIFIER_TABLE:
3082 case IDENTIFIER_OFFSET:
3083 case INTERESTING_IDENTIFIERS:
3084 case STATISTICS:
3085 case PP_CONDITIONAL_STACK:
3086 case PP_COUNTER_VALUE:
3087 case SOURCE_LOCATION_OFFSETS:
3088 case MODULE_OFFSET_MAP:
3089 case SOURCE_MANAGER_LINE_TABLE:
3090 case SOURCE_LOCATION_PRELOADS:
3091 case PPD_ENTITIES_OFFSETS:
3092 case HEADER_SEARCH_TABLE:
3093 case IMPORTED_MODULES:
3094 case MACRO_OFFSET:
3095 break;
3096 default:
3097 continue;
3098 }
3099 }
3100
3101 switch (RecordType) {
3102 default: // Default behavior: ignore.
3103 break;
3104
3105 case TYPE_OFFSET: {
3106 if (F.LocalNumTypes != 0) {
3107 Error("duplicate TYPE_OFFSET record in AST file");
3108 return Failure;
3109 }
3110 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3111 F.LocalNumTypes = Record[0];
3112 unsigned LocalBaseTypeIndex = Record[1];
3113 F.BaseTypeIndex = getTotalNumTypes();
3114
3115 if (F.LocalNumTypes > 0) {
3116 // Introduce the global -> local mapping for types within this module.
3117 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3118
3119 // Introduce the local -> global mapping for types within this module.
3120 F.TypeRemap.insertOrReplace(
3121 std::make_pair(LocalBaseTypeIndex,
3122 F.BaseTypeIndex - LocalBaseTypeIndex));
3123
3124 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3125 }
3126 break;
3127 }
3128
3129 case DECL_OFFSET: {
3130 if (F.LocalNumDecls != 0) {
3131 Error("duplicate DECL_OFFSET record in AST file");
3132 return Failure;
3133 }
3134 F.DeclOffsets = (const DeclOffset *)Blob.data();
3135 F.LocalNumDecls = Record[0];
3136 unsigned LocalBaseDeclID = Record[1];
3137 F.BaseDeclID = getTotalNumDecls();
3138
3139 if (F.LocalNumDecls > 0) {
3140 // Introduce the global -> local mapping for declarations within this
3141 // module.
3142 GlobalDeclMap.insert(
3143 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3144
3145 // Introduce the local -> global mapping for declarations within this
3146 // module.
3147 F.DeclRemap.insertOrReplace(
3148 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3149
3150 // Introduce the global -> local mapping for declarations within this
3151 // module.
3152 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3153
3154 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3155 }
3156 break;
3157 }
3158
3159 case TU_UPDATE_LEXICAL: {
3160 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3161 LexicalContents Contents(
3162 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3163 Blob.data()),
3164 static_cast<unsigned int>(Blob.size() / 4));
3165 TULexicalDecls.push_back(std::make_pair(&F, Contents));
3166 TU->setHasExternalLexicalStorage(true);
3167 break;
3168 }
3169
3170 case UPDATE_VISIBLE: {
3171 unsigned Idx = 0;
3172 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3173 auto *Data = (const unsigned char*)Blob.data();
3174 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3175 // If we've already loaded the decl, perform the updates when we finish
3176 // loading this block.
3177 if (Decl *D = GetExistingDecl(ID))
3178 PendingUpdateRecords.push_back(
3179 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3180 break;
3181 }
3182
3183 case IDENTIFIER_TABLE:
3184 F.IdentifierTableData = Blob.data();
3185 if (Record[0]) {
3186 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3187 (const unsigned char *)F.IdentifierTableData + Record[0],
3188 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3189 (const unsigned char *)F.IdentifierTableData,
3190 ASTIdentifierLookupTrait(*this, F));
3191
3192 PP.getIdentifierTable().setExternalIdentifierLookup(this);
3193 }
3194 break;
3195
3196 case IDENTIFIER_OFFSET: {
3197 if (F.LocalNumIdentifiers != 0) {
3198 Error("duplicate IDENTIFIER_OFFSET record in AST file");
3199 return Failure;
3200 }
3201 F.IdentifierOffsets = (const uint32_t *)Blob.data();
3202 F.LocalNumIdentifiers = Record[0];
3203 unsigned LocalBaseIdentifierID = Record[1];
3204 F.BaseIdentifierID = getTotalNumIdentifiers();
3205
3206 if (F.LocalNumIdentifiers > 0) {
3207 // Introduce the global -> local mapping for identifiers within this
3208 // module.
3209 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3210 &F));
3211
3212 // Introduce the local -> global mapping for identifiers within this
3213 // module.
3214 F.IdentifierRemap.insertOrReplace(
3215 std::make_pair(LocalBaseIdentifierID,
3216 F.BaseIdentifierID - LocalBaseIdentifierID));
3217
3218 IdentifiersLoaded.resize(IdentifiersLoaded.size()
3219 + F.LocalNumIdentifiers);
3220 }
3221 break;
3222 }
3223
3224 case INTERESTING_IDENTIFIERS:
3225 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3226 break;
3227
3228 case EAGERLY_DESERIALIZED_DECLS:
3229 // FIXME: Skip reading this record if our ASTConsumer doesn't care
3230 // about "interesting" decls (for instance, if we're building a module).
3231 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3232 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3233 break;
3234
3235 case MODULAR_CODEGEN_DECLS:
3236 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3237 // them (ie: if we're not codegenerating this module).
3238 if (F.Kind == MK_MainFile ||
3239 getContext().getLangOpts().BuildingPCHWithObjectFile)
3240 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3241 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3242 break;
3243
3244 case SPECIAL_TYPES:
3245 if (SpecialTypes.empty()) {
3246 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3247 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3248 break;
3249 }
3250
3251 if (SpecialTypes.size() != Record.size()) {
3252 Error("invalid special-types record");
3253 return Failure;
3254 }
3255
3256 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3257 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3258 if (!SpecialTypes[I])
3259 SpecialTypes[I] = ID;
3260 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3261 // merge step?
3262 }
3263 break;
3264
3265 case STATISTICS:
3266 TotalNumStatements += Record[0];
3267 TotalNumMacros += Record[1];
3268 TotalLexicalDeclContexts += Record[2];
3269 TotalVisibleDeclContexts += Record[3];
3270 break;
3271
3272 case UNUSED_FILESCOPED_DECLS:
3273 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3274 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3275 break;
3276
3277 case DELEGATING_CTORS:
3278 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3279 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3280 break;
3281
3282 case WEAK_UNDECLARED_IDENTIFIERS:
3283 if (Record.size() % 4 != 0) {
3284 Error("invalid weak identifiers record");
3285 return Failure;
3286 }
3287
3288 // FIXME: Ignore weak undeclared identifiers from non-original PCH
3289 // files. This isn't the way to do it :)
3290 WeakUndeclaredIdentifiers.clear();
3291
3292 // Translate the weak, undeclared identifiers into global IDs.
3293 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3294 WeakUndeclaredIdentifiers.push_back(
3295 getGlobalIdentifierID(F, Record[I++]));
3296 WeakUndeclaredIdentifiers.push_back(
3297 getGlobalIdentifierID(F, Record[I++]));
3298 WeakUndeclaredIdentifiers.push_back(
3299 ReadSourceLocation(F, Record, I).getRawEncoding());
3300 WeakUndeclaredIdentifiers.push_back(Record[I++]);
3301 }
3302 break;
3303
3304 case SELECTOR_OFFSETS: {
3305 F.SelectorOffsets = (const uint32_t *)Blob.data();
3306 F.LocalNumSelectors = Record[0];
3307 unsigned LocalBaseSelectorID = Record[1];
3308 F.BaseSelectorID = getTotalNumSelectors();
3309
3310 if (F.LocalNumSelectors > 0) {
3311 // Introduce the global -> local mapping for selectors within this
3312 // module.
3313 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3314
3315 // Introduce the local -> global mapping for selectors within this
3316 // module.
3317 F.SelectorRemap.insertOrReplace(
3318 std::make_pair(LocalBaseSelectorID,
3319 F.BaseSelectorID - LocalBaseSelectorID));
3320
3321 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3322 }
3323 break;
3324 }
3325
3326 case METHOD_POOL:
3327 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3328 if (Record[0])
3329 F.SelectorLookupTable
3330 = ASTSelectorLookupTable::Create(
3331 F.SelectorLookupTableData + Record[0],
3332 F.SelectorLookupTableData,
3333 ASTSelectorLookupTrait(*this, F));
3334 TotalNumMethodPoolEntries += Record[1];
3335 break;
3336
3337 case REFERENCED_SELECTOR_POOL:
3338 if (!Record.empty()) {
3339 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3340 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3341 Record[Idx++]));
3342 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3343 getRawEncoding());
3344 }
3345 }
3346 break;
3347
3348 case PP_CONDITIONAL_STACK:
3349 if (!Record.empty()) {
3350 unsigned Idx = 0, End = Record.size() - 1;
3351 bool ReachedEOFWhileSkipping = Record[Idx++];
3352 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3353 if (ReachedEOFWhileSkipping) {
3354 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3355 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3356 bool FoundNonSkipPortion = Record[Idx++];
3357 bool FoundElse = Record[Idx++];
3358 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3359 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3360 FoundElse, ElseLoc);
3361 }
3362 SmallVector<PPConditionalInfo, 4> ConditionalStack;
3363 while (Idx < End) {
3364 auto Loc = ReadSourceLocation(F, Record, Idx);
3365 bool WasSkipping = Record[Idx++];
3366 bool FoundNonSkip = Record[Idx++];
3367 bool FoundElse = Record[Idx++];
3368 ConditionalStack.push_back(
3369 {Loc, WasSkipping, FoundNonSkip, FoundElse});
3370 }
3371 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3372 }
3373 break;
3374
3375 case PP_COUNTER_VALUE:
3376 if (!Record.empty() && Listener)
3377 Listener->ReadCounter(F, Record[0]);
3378 break;
3379
3380 case FILE_SORTED_DECLS:
3381 F.FileSortedDecls = (const DeclID *)Blob.data();
3382 F.NumFileSortedDecls = Record[0];
3383 break;
3384
3385 case SOURCE_LOCATION_OFFSETS: {
3386 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3387 F.LocalNumSLocEntries = Record[0];
3388 unsigned SLocSpaceSize = Record[1];
3389 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3390 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3391 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3392 SLocSpaceSize);
3393 if (!F.SLocEntryBaseID) {
3394 Error("ran out of source locations");
3395 break;
3396 }
3397 // Make our entry in the range map. BaseID is negative and growing, so
3398 // we invert it. Because we invert it, though, we need the other end of
3399 // the range.
3400 unsigned RangeStart =
3401 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3402 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3403 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3404
3405 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3406 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3407 GlobalSLocOffsetMap.insert(
3408 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3409 - SLocSpaceSize,&F));
3410
3411 // Initialize the remapping table.
3412 // Invalid stays invalid.
3413 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3414 // This module. Base was 2 when being compiled.
3415 F.SLocRemap.insertOrReplace(std::make_pair(2U,
3416 static_cast<int>(F.SLocEntryBaseOffset - 2)));
3417
3418 TotalNumSLocEntries += F.LocalNumSLocEntries;
3419 break;
3420 }
3421
3422 case MODULE_OFFSET_MAP:
3423 F.ModuleOffsetMap = Blob;
3424 break;
3425
3426 case SOURCE_MANAGER_LINE_TABLE:
3427 if (ParseLineTable(F, Record)) {
3428 Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
3429 return Failure;
3430 }
3431 break;
3432
3433 case SOURCE_LOCATION_PRELOADS: {
3434 // Need to transform from the local view (1-based IDs) to the global view,
3435 // which is based off F.SLocEntryBaseID.
3436 if (!F.PreloadSLocEntries.empty()) {
3437 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3438 return Failure;
3439 }
3440
3441 F.PreloadSLocEntries.swap(Record);
3442 break;
3443 }
3444
3445 case EXT_VECTOR_DECLS:
3446 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3447 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3448 break;
3449
3450 case VTABLE_USES:
3451 if (Record.size() % 3 != 0) {
3452 Error("Invalid VTABLE_USES record");
3453 return Failure;
3454 }
3455
3456 // Later tables overwrite earlier ones.
3457 // FIXME: Modules will have some trouble with this. This is clearly not
3458 // the right way to do this.
3459 VTableUses.clear();
3460
3461 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3462 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3463 VTableUses.push_back(
3464 ReadSourceLocation(F, Record, Idx).getRawEncoding());
3465 VTableUses.push_back(Record[Idx++]);
3466 }
3467 break;
3468
3469 case PENDING_IMPLICIT_INSTANTIATIONS:
3470 if (PendingInstantiations.size() % 2 != 0) {
3471 Error("Invalid existing PendingInstantiations");
3472 return Failure;
3473 }
3474
3475 if (Record.size() % 2 != 0) {
3476 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3477 return Failure;
3478 }
3479
3480 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3481 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3482 PendingInstantiations.push_back(
3483 ReadSourceLocation(F, Record, I).getRawEncoding());
3484 }
3485 break;
3486
3487 case SEMA_DECL_REFS:
3488 if (Record.size() != 3) {
3489 Error("Invalid SEMA_DECL_REFS block");
3490 return Failure;
3491 }
3492 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3493 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3494 break;
3495
3496 case PPD_ENTITIES_OFFSETS: {
3497 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3498 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3499 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3500
3501 unsigned LocalBasePreprocessedEntityID = Record[0];
3502
3503 unsigned StartingID;
3504 if (!PP.getPreprocessingRecord())
3505 PP.createPreprocessingRecord();
3506 if (!PP.getPreprocessingRecord()->getExternalSource())
3507 PP.getPreprocessingRecord()->SetExternalSource(*this);
3508 StartingID
3509 = PP.getPreprocessingRecord()
3510 ->allocateLoadedEntities(F.NumPreprocessedEntities);
3511 F.BasePreprocessedEntityID = StartingID;
3512
3513 if (F.NumPreprocessedEntities > 0) {
3514 // Introduce the global -> local mapping for preprocessed entities in
3515 // this module.
3516 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3517
3518 // Introduce the local -> global mapping for preprocessed entities in
3519 // this module.
3520 F.PreprocessedEntityRemap.insertOrReplace(
3521 std::make_pair(LocalBasePreprocessedEntityID,
3522 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3523 }
3524
3525 break;
3526 }
3527
3528 case PPD_SKIPPED_RANGES: {
3529 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3530 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3531 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3532
3533 if (!PP.getPreprocessingRecord())
3534 PP.createPreprocessingRecord();
3535 if (!PP.getPreprocessingRecord()->getExternalSource())
3536 PP.getPreprocessingRecord()->SetExternalSource(*this);
3537 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3538 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3539
3540 if (F.NumPreprocessedSkippedRanges > 0)
3541 GlobalSkippedRangeMap.insert(
3542 std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3543 break;
3544 }
3545
3546 case DECL_UPDATE_OFFSETS:
3547 if (Record.size() % 2 != 0) {
3548 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3549 return Failure;
3550 }
3551 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3552 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3553 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3554
3555 // If we've already loaded the decl, perform the updates when we finish
3556 // loading this block.
3557 if (Decl *D = GetExistingDecl(ID))
3558 PendingUpdateRecords.push_back(
3559 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3560 }
3561 break;
3562
3563 case OBJC_CATEGORIES_MAP:
3564 if (F.LocalNumObjCCategoriesInMap != 0) {
3565 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3566 return Failure;
3567 }
3568
3569 F.LocalNumObjCCategoriesInMap = Record[0];
3570 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3571 break;
3572
3573 case OBJC_CATEGORIES:
3574 F.ObjCCategories.swap(Record);
3575 break;
3576
3577 case CUDA_SPECIAL_DECL_REFS:
3578 // Later tables overwrite earlier ones.
3579 // FIXME: Modules will have trouble with this.
3580 CUDASpecialDeclRefs.clear();
3581 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3582 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3583 break;
3584
3585 case HEADER_SEARCH_TABLE:
3586 F.HeaderFileInfoTableData = Blob.data();
3587 F.LocalNumHeaderFileInfos = Record[1];
3588 if (Record[0]) {
3589 F.HeaderFileInfoTable
3590 = HeaderFileInfoLookupTable::Create(
3591 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3592 (const unsigned char *)F.HeaderFileInfoTableData,
3593 HeaderFileInfoTrait(*this, F,
3594 &PP.getHeaderSearchInfo(),
3595 Blob.data() + Record[2]));
3596
3597 PP.getHeaderSearchInfo().SetExternalSource(this);
3598 if (!PP.getHeaderSearchInfo().getExternalLookup())
3599 PP.getHeaderSearchInfo().SetExternalLookup(this);
3600 }
3601 break;
3602
3603 case FP_PRAGMA_OPTIONS:
3604 // Later tables overwrite earlier ones.
3605 FPPragmaOptions.swap(Record);
3606 break;
3607
3608 case OPENCL_EXTENSIONS:
3609 for (unsigned I = 0, E = Record.size(); I != E; ) {
3610 auto Name = ReadString(Record, I);
3611 auto &OptInfo = OpenCLExtensions.OptMap[Name];
3612 OptInfo.Supported = Record[I++] != 0;
3613 OptInfo.Enabled = Record[I++] != 0;
3614 OptInfo.Avail = Record[I++];
3615 OptInfo.Core = Record[I++];
3616 OptInfo.Opt = Record[I++];
3617 }
3618 break;
3619
3620 case OPENCL_EXTENSION_TYPES:
3621 for (unsigned I = 0, E = Record.size(); I != E;) {
3622 auto TypeID = static_cast<::TypeID>(Record[I++]);
3623 auto *Type = GetType(TypeID).getTypePtr();
3624 auto NumExt = static_cast<unsigned>(Record[I++]);
3625 for (unsigned II = 0; II != NumExt; ++II) {
3626 auto Ext = ReadString(Record, I);
3627 OpenCLTypeExtMap[Type].insert(Ext);
3628 }
3629 }
3630 break;
3631
3632 case OPENCL_EXTENSION_DECLS:
3633 for (unsigned I = 0, E = Record.size(); I != E;) {
3634 auto DeclID = static_cast<::DeclID>(Record[I++]);
3635 auto *Decl = GetDecl(DeclID);
3636 auto NumExt = static_cast<unsigned>(Record[I++]);
3637 for (unsigned II = 0; II != NumExt; ++II) {
3638 auto Ext = ReadString(Record, I);
3639 OpenCLDeclExtMap[Decl].insert(Ext);
3640 }
3641 }
3642 break;
3643
3644 case TENTATIVE_DEFINITIONS:
3645 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3646 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3647 break;
3648
3649 case KNOWN_NAMESPACES:
3650 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3651 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3652 break;
3653
3654 case UNDEFINED_BUT_USED:
3655 if (UndefinedButUsed.size() % 2 != 0) {
3656 Error("Invalid existing UndefinedButUsed");
3657 return Failure;
3658 }
3659
3660 if (Record.size() % 2 != 0) {
3661 Error("invalid undefined-but-used record");
3662 return Failure;
3663 }
3664 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3665 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3666 UndefinedButUsed.push_back(
3667 ReadSourceLocation(F, Record, I).getRawEncoding());
3668 }
3669 break;
3670
3671 case DELETE_EXPRS_TO_ANALYZE:
3672 for (unsigned I = 0, N = Record.size(); I != N;) {
3673 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3674 const uint64_t Count = Record[I++];
3675 DelayedDeleteExprs.push_back(Count);
3676 for (uint64_t C = 0; C < Count; ++C) {
3677 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3678 bool IsArrayForm = Record[I++] == 1;
3679 DelayedDeleteExprs.push_back(IsArrayForm);
3680 }
3681 }
3682 break;
3683
3684 case IMPORTED_MODULES:
3685 if (!F.isModule()) {
3686 // If we aren't loading a module (which has its own exports), make
3687 // all of the imported modules visible.
3688 // FIXME: Deal with macros-only imports.
3689 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3690 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3691 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3692 if (GlobalID) {
3693 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3694 if (DeserializationListener)
3695 DeserializationListener->ModuleImportRead(GlobalID, Loc);
3696 }
3697 }
3698 }
3699 break;
3700
3701 case MACRO_OFFSET: {
3702 if (F.LocalNumMacros != 0) {
3703 Error("duplicate MACRO_OFFSET record in AST file");
3704 return Failure;
3705 }
3706 F.MacroOffsets = (const uint32_t *)Blob.data();
3707 F.LocalNumMacros = Record[0];
3708 unsigned LocalBaseMacroID = Record[1];
3709 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3710 F.BaseMacroID = getTotalNumMacros();
3711
3712 if (F.LocalNumMacros > 0) {
3713 // Introduce the global -> local mapping for macros within this module.
3714 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3715
3716 // Introduce the local -> global mapping for macros within this module.
3717 F.MacroRemap.insertOrReplace(
3718 std::make_pair(LocalBaseMacroID,
3719 F.BaseMacroID - LocalBaseMacroID));
3720
3721 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3722 }
3723 break;
3724 }
3725
3726 case LATE_PARSED_TEMPLATE:
3727 LateParsedTemplates.emplace_back(
3728 std::piecewise_construct, std::forward_as_tuple(&F),
3729 std::forward_as_tuple(Record.begin(), Record.end()));
3730 break;
3731
3732 case OPTIMIZE_PRAGMA_OPTIONS:
3733 if (Record.size() != 1) {
3734 Error("invalid pragma optimize record");
3735 return Failure;
3736 }
3737 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3738 break;
3739
3740 case MSSTRUCT_PRAGMA_OPTIONS:
3741 if (Record.size() != 1) {
3742 Error("invalid pragma ms_struct record");
3743 return Failure;
3744 }
3745 PragmaMSStructState = Record[0];
3746 break;
3747
3748 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3749 if (Record.size() != 2) {
3750 Error("invalid pragma ms_struct record");
3751 return Failure;
3752 }
3753 PragmaMSPointersToMembersState = Record[0];
3754 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3755 break;
3756
3757 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3758 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3759 UnusedLocalTypedefNameCandidates.push_back(
3760 getGlobalDeclID(F, Record[I]));
3761 break;
3762
3763 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3764 if (Record.size() != 1) {
3765 Error("invalid cuda pragma options record");
3766 return Failure;
3767 }
3768 ForceCUDAHostDeviceDepth = Record[0];
3769 break;
3770
3771 case ALIGN_PACK_PRAGMA_OPTIONS: {
3772 if (Record.size() < 3) {
3773 Error("invalid pragma pack record");
3774 return Failure;
3775 }
3776 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);
3777 PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3778 unsigned NumStackEntries = Record[2];
3779 unsigned Idx = 3;
3780 // Reset the stack when importing a new module.
3781 PragmaAlignPackStack.clear();
3782 for (unsigned I = 0; I < NumStackEntries; ++I) {
3783 PragmaAlignPackStackEntry Entry;
3784 Entry.Value = ReadAlignPackInfo(Record[Idx++]);
3785 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3786 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3787 PragmaAlignPackStrings.push_back(ReadString(Record, Idx));
3788 Entry.SlotLabel = PragmaAlignPackStrings.back();
3789 PragmaAlignPackStack.push_back(Entry);
3790 }
3791 break;
3792 }
3793
3794 case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3795 if (Record.size() < 3) {
3796 Error("invalid pragma pack record");
3797 return Failure;
3798 }
3799 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3800 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3801 unsigned NumStackEntries = Record[2];
3802 unsigned Idx = 3;
3803 // Reset the stack when importing a new module.
3804 FpPragmaStack.clear();
3805 for (unsigned I = 0; I < NumStackEntries; ++I) {
3806 FpPragmaStackEntry Entry;
3807 Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
3808 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3809 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3810 FpPragmaStrings.push_back(ReadString(Record, Idx));
3811 Entry.SlotLabel = FpPragmaStrings.back();
3812 FpPragmaStack.push_back(Entry);
3813 }
3814 break;
3815 }
3816
3817 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3818 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3819 DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I]));
3820 break;
3821 }
3822 }
3823}
3824
3825void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3826 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3827
3828 // Additional remapping information.
3829 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3830 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3831 F.ModuleOffsetMap = StringRef();
3832
3833 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3834 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3835 F.SLocRemap.insert(std::make_pair(0U, 0));
3836 F.SLocRemap.insert(std::make_pair(2U, 1));
3837 }
3838
3839 // Continuous range maps we may be updating in our module.
3840 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3841 RemapBuilder SLocRemap(F.SLocRemap);
3842 RemapBuilder IdentifierRemap(F.IdentifierRemap);
3843 RemapBuilder MacroRemap(F.MacroRemap);
3844 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3845 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3846 RemapBuilder SelectorRemap(F.SelectorRemap);
3847 RemapBuilder DeclRemap(F.DeclRemap);
3848 RemapBuilder TypeRemap(F.TypeRemap);
3849
3850 while (Data < DataEnd) {
3851 // FIXME: Looking up dependency modules by filename is horrible. Let's
3852 // start fixing this with prebuilt, explicit and implicit modules and see
3853 // how it goes...
3854 using namespace llvm::support;
3855 ModuleKind Kind = static_cast<ModuleKind>(
3856 endian::readNext<uint8_t, little, unaligned>(Data));
3857 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3858 StringRef Name = StringRef((const char*)Data, Len);
3859 Data += Len;
3860 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3861 Kind == MK_ImplicitModule
3862 ? ModuleMgr.lookupByModuleName(Name)
3863 : ModuleMgr.lookupByFileName(Name));
3864 if (!OM) {
3865 std::string Msg =
3866 "SourceLocation remap refers to unknown module, cannot find ";
3867 Msg.append(std::string(Name));
3868 Error(Msg);
3869 return;
3870 }
3871
3872 uint32_t SLocOffset =
3873 endian::readNext<uint32_t, little, unaligned>(Data);
3874 uint32_t IdentifierIDOffset =
3875 endian::readNext<uint32_t, little, unaligned>(Data);
3876 uint32_t MacroIDOffset =
3877 endian::readNext<uint32_t, little, unaligned>(Data);
3878 uint32_t PreprocessedEntityIDOffset =
3879 endian::readNext<uint32_t, little, unaligned>(Data);
3880 uint32_t SubmoduleIDOffset =
3881 endian::readNext<uint32_t, little, unaligned>(Data);
3882 uint32_t SelectorIDOffset =
3883 endian::readNext<uint32_t, little, unaligned>(Data);
3884 uint32_t DeclIDOffset =
3885 endian::readNext<uint32_t, little, unaligned>(Data);
3886 uint32_t TypeIndexOffset =
3887 endian::readNext<uint32_t, little, unaligned>(Data);
3888
3889 uint32_t None = std::numeric_limits<uint32_t>::max();
3890
3891 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3892 RemapBuilder &Remap) {
3893 if (Offset != None)
3894 Remap.insert(std::make_pair(Offset,
3895 static_cast<int>(BaseOffset - Offset)));
3896 };
3897 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3898 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3899 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3900 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3901 PreprocessedEntityRemap);
3902 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3903 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3904 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3905 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3906
3907 // Global -> local mappings.
3908 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3909 }
3910}
3911
3912ASTReader::ASTReadResult
3913ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3914 const ModuleFile *ImportedBy,
3915 unsigned ClientLoadCapabilities) {
3916 unsigned Idx = 0;
3917 F.ModuleMapPath = ReadPath(F, Record, Idx);
3918
3919 // Try to resolve ModuleName in the current header search context and
3920 // verify that it is found in the same module map file as we saved. If the
3921 // top-level AST file is a main file, skip this check because there is no
3922 // usable header search context.
3923 assert(!F.ModuleName.empty() &&
3924 "MODULE_NAME should come before MODULE_MAP_FILE");
3925 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3926 // An implicitly-loaded module file should have its module listed in some
3927 // module map file that we've already loaded.
3928 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3929 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3930 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3931 // Don't emit module relocation error if we have -fno-validate-pch
3932 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3933 DisableValidationForModuleKind::Module) &&
3934 !ModMap) {
3935 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3936 if (auto ASTFE = M ? M->getASTFile() : None) {
3937 // This module was defined by an imported (explicit) module.
3938 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3939 << ASTFE->getName();
3940 } else {
3941 // This module was built with a different module map.
3942 Diag(diag::err_imported_module_not_found)
3943 << F.ModuleName << F.FileName
3944 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3945 << !ImportedBy;
3946 // In case it was imported by a PCH, there's a chance the user is
3947 // just missing to include the search path to the directory containing
3948 // the modulemap.
3949 if (ImportedBy && ImportedBy->Kind == MK_PCH)
3950 Diag(diag::note_imported_by_pch_module_not_found)
3951 << llvm::sys::path::parent_path(F.ModuleMapPath);
3952 }
3953 }
3954 return OutOfDate;
3955 }
3956
3957 assert(M && M->Name == F.ModuleName && "found module with different name");
3958
3959 // Check the primary module map file.
3960 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3961 if (!StoredModMap || *StoredModMap != ModMap) {
3962 assert(ModMap && "found module is missing module map file");
3963 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3964 "top-level import should be verified");
3965 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3966 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3967 Diag(diag::err_imported_module_modmap_changed)
3968 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3969 << ModMap->getName() << F.ModuleMapPath << NotImported;
3970 return OutOfDate;
3971 }
3972
3973 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3974 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3975 // FIXME: we should use input files rather than storing names.
3976 std::string Filename = ReadPath(F, Record, Idx);
3977 auto F = FileMgr.getFile(Filename, false, false);
3978 if (!F) {
3979 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3980 Error("could not find file '" + Filename +"' referenced by AST file");
3981 return OutOfDate;
3982 }
3983 AdditionalStoredMaps.insert(*F);
3984 }
3985
3986 // Check any additional module map files (e.g. module.private.modulemap)
3987 // that are not in the pcm.
3988 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3989 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3990 // Remove files that match
3991 // Note: SmallPtrSet::erase is really remove
3992 if (!AdditionalStoredMaps.erase(ModMap)) {
3993 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3994 Diag(diag::err_module_different_modmap)
3995 << F.ModuleName << /*new*/0 << ModMap->getName();
3996 return OutOfDate;
3997 }
3998 }
3999 }
4000
4001 // Check any additional module map files that are in the pcm, but not
4002 // found in header search. Cases that match are already removed.
4003 for (const FileEntry *ModMap : AdditionalStoredMaps) {
4004 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4005 Diag(diag::err_module_different_modmap)
4006 << F.ModuleName << /*not new*/1 << ModMap->getName();
4007 return OutOfDate;
4008 }
4009 }
4010
4011 if (Listener)
4012 Listener->ReadModuleMapFile(F.ModuleMapPath);
4013 return Success;
4014}
4015
4016/// Move the given method to the back of the global list of methods.
4017static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4018 // Find the entry for this selector in the method pool.
4019 Sema::GlobalMethodPool::iterator Known
4020 = S.MethodPool.find(Method->getSelector());
4021 if (Known == S.MethodPool.end())
4022 return;
4023
4024 // Retrieve the appropriate method list.
4025 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4026 : Known->second.second;
4027 bool Found = false;
4028 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4029 if (!Found) {
4030 if (List->getMethod() == Method) {
4031 Found = true;
4032 } else {
4033 // Keep searching.
4034 continue;
4035 }
4036 }
4037
4038 if (List->getNext())
4039 List->setMethod(List->getNext()->getMethod());
4040 else
4041 List->setMethod(Method);
4042 }
4043}
4044
4045void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4046 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4047 for (Decl *D : Names) {
4048 bool wasHidden = !D->isUnconditionallyVisible();
4049 D->setVisibleDespiteOwningModule();
4050
4051 if (wasHidden && SemaObj) {
4052 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4053 moveMethodToBackOfGlobalList(*SemaObj, Method);
4054 }
4055 }
4056 }
4057}
4058
4059void ASTReader::makeModuleVisible(Module *Mod,
4060 Module::NameVisibilityKind NameVisibility,
4061 SourceLocation ImportLoc) {
4062 llvm::SmallPtrSet<Module *, 4> Visited;
4063 SmallVector<Module *, 4> Stack;
4064 Stack.push_back(Mod);
4065 while (!Stack.empty()) {
4066 Mod = Stack.pop_back_val();
4067
4068 if (NameVisibility <= Mod->NameVisibility) {
4069 // This module already has this level of visibility (or greater), so
4070 // there is nothing more to do.
4071 continue;
4072 }
4073
4074 if (Mod->isUnimportable()) {
4075 // Modules that aren't importable cannot be made visible.
4076 continue;
4077 }
4078
4079 // Update the module's name visibility.
4080 Mod->NameVisibility = NameVisibility;
4081
4082 // If we've already deserialized any names from this module,
4083 // mark them as visible.
4084 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4085 if (Hidden != HiddenNamesMap.end()) {
4086 auto HiddenNames = std::move(*Hidden);
4087 HiddenNamesMap.erase(Hidden);
4088 makeNamesVisible(HiddenNames.second, HiddenNames.first);
4089 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4090 "making names visible added hidden names");
4091 }
4092
4093 // Push any exported modules onto the stack to be marked as visible.
4094 SmallVector<Module *, 16> Exports;
4095 Mod->getExportedModules(Exports);
4096 for (SmallVectorImpl<Module *>::iterator
4097 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4098 Module *Exported = *I;
4099 if (Visited.insert(Exported).second)
4100 Stack.push_back(Exported);
4101 }
4102 }
4103}
4104
4105/// We've merged the definition \p MergedDef into the existing definition
4106/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4107/// visible.
4108void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4109 NamedDecl *MergedDef) {
4110 if (!Def->isUnconditionallyVisible()) {
4111 // If MergedDef is visible or becomes visible, make the definition visible.
4112 if (MergedDef->isUnconditionallyVisible())
4113 Def->setVisibleDespiteOwningModule();
4114 else {
4115 getContext().mergeDefinitionIntoModule(
4116 Def, MergedDef->getImportedOwningModule(),
4117 /*NotifyListeners*/ false);
4118 PendingMergedDefinitionsToDeduplicate.insert(Def);
4119 }
4120 }
4121}
4122
4123bool ASTReader::loadGlobalIndex() {
4124 if (GlobalIndex)
4125 return false;
4126
4127 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4128 !PP.getLangOpts().Modules)
4129 return true;
4130
4131 // Try to load the global index.
4132 TriedLoadingGlobalIndex = true;
4133 StringRef ModuleCachePath
4134 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4135 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4136 GlobalModuleIndex::readIndex(ModuleCachePath);
4137 if (llvm::Error Err = std::move(Result.second)) {
4138 assert(!Result.first);
4139 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4140 return true;
4141 }
4142
4143 GlobalIndex.reset(Result.first);
4144 ModuleMgr.setGlobalIndex(GlobalIndex.get());
4145 return false;
4146}
4147
4148bool ASTReader::isGlobalIndexUnavailable() const {
4149 return PP.getLangOpts().Modules && UseGlobalIndex &&
4150 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4151}
4152
4153static void updateModuleTimestamp(ModuleFile &MF) {
4154 // Overwrite the timestamp file contents so that file's mtime changes.
4155 std::string TimestampFilename = MF.getTimestampFilename();
4156 std::error_code EC;
4157 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4158 if (EC)
4159 return;
4160 OS << "Timestamp file\n";
4161 OS.close();
4162 OS.clear_error(); // Avoid triggering a fatal error.
4163}
4164
4165/// Given a cursor at the start of an AST file, scan ahead and drop the
4166/// cursor into the start of the given block ID, returning false on success and
4167/// true on failure.
4168static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4169 while (true) {
4170 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4171 if (!MaybeEntry) {
4172 // FIXME this drops errors on the floor.
4173 consumeError(MaybeEntry.takeError());
4174 return true;
4175 }
4176 llvm::BitstreamEntry Entry = MaybeEntry.get();
4177
4178 switch (Entry.Kind) {
4179 case llvm::BitstreamEntry::Error:
4180 case llvm::BitstreamEntry::EndBlock:
4181 return true;
4182
4183 case llvm::BitstreamEntry::Record:
4184 // Ignore top-level records.
4185 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4186 break;
4187 else {
4188 // FIXME this drops errors on the floor.
4189 consumeError(Skipped.takeError());
4190 return true;
4191 }
4192
4193 case llvm::BitstreamEntry::SubBlock:
4194 if (Entry.ID == BlockID) {
4195 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4196 // FIXME this drops the error on the floor.
4197 consumeError(std::move(Err));
4198 return true;
4199 }
4200 // Found it!
4201 return false;
4202 }
4203
4204 if (llvm::Error Err = Cursor.SkipBlock()) {
4205 // FIXME this drops the error on the floor.
4206 consumeError(std::move(Err));
4207 return true;
4208 }
4209 }
4210 }
4211}
4212
4213ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4214 ModuleKind Type,
4215 SourceLocation ImportLoc,
4216 unsigned ClientLoadCapabilities,
4217 SmallVectorImpl<ImportedSubmodule> *Imported) {
4218 llvm::SaveAndRestore<SourceLocation>
4219 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4220 llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII(
4221 CurrentDeserializingModuleKind, Type);
4222
4223 // Defer any pending actions until we get to the end of reading the AST file.
4224 Deserializing AnASTFile(this);
4225
4226 // Bump the generation number.
4227 unsigned PreviousGeneration = 0;
4228 if (ContextObj)
4229 PreviousGeneration = incrementGeneration(*ContextObj);
4230
4231 unsigned NumModules = ModuleMgr.size();
4232 auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4233 assert(ReadResult && "expected to return error");
4234 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4235 PP.getLangOpts().Modules
4236 ? &PP.getHeaderSearchInfo().getModuleMap()
4237 : nullptr);
4238
4239 // If we find that any modules are unusable, the global index is going
4240 // to be out-of-date. Just remove it.
4241 GlobalIndex.reset();
4242 ModuleMgr.setGlobalIndex(nullptr);
4243 return ReadResult;
4244 };
4245
4246 SmallVector<ImportedModule, 4> Loaded;
4247 switch (ASTReadResult ReadResult =
4248 ReadASTCore(FileName, Type, ImportLoc,
4249 /*ImportedBy=*/nullptr, Loaded, 0, 0,
4250 ASTFileSignature(), ClientLoadCapabilities)) {
4251 case Failure:
4252 case Missing:
4253 case OutOfDate:
4254 case VersionMismatch:
4255 case ConfigurationMismatch:
4256 case HadErrors:
4257 return removeModulesAndReturn(ReadResult);
4258 case Success:
4259 break;
4260 }
4261
4262 // Here comes stuff that we only do once the entire chain is loaded.
4263
4264 // Load the AST blocks of all of the modules that we loaded. We can still
4265 // hit errors parsing the ASTs at this point.
4266 for (ImportedModule &M : Loaded) {
4267 ModuleFile &F = *M.Mod;
4268
4269 // Read the AST block.
4270 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4271 return removeModulesAndReturn(Result);
4272
4273 // The AST block should always have a definition for the main module.
4274 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4275 Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4276 return removeModulesAndReturn(Failure);
4277 }
4278
4279 // Read the extension blocks.
4280 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4281 if (ASTReadResult Result = ReadExtensionBlock(F))
4282 return removeModulesAndReturn(Result);
4283 }
4284
4285 // Once read, set the ModuleFile bit base offset and update the size in
4286 // bits of all files we've seen.
4287 F.GlobalBitOffset = TotalModulesSizeInBits;
4288 TotalModulesSizeInBits += F.SizeInBits;
4289 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4290 }
4291
4292 // Preload source locations and interesting indentifiers.
4293 for (ImportedModule &M : Loaded) {
4294 ModuleFile &F = *M.Mod;
4295
4296 // Preload SLocEntries.
4297 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4298 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4299 // Load it through the SourceManager and don't call ReadSLocEntry()
4300 // directly because the entry may have already been loaded in which case
4301 // calling ReadSLocEntry() directly would trigger an assertion in
4302 // SourceManager.
4303 SourceMgr.getLoadedSLocEntryByID(Index);
4304 }
4305
4306 // Map the original source file ID into the ID space of the current
4307 // compilation.
4308 if (F.OriginalSourceFileID.isValid()) {
4309 F.OriginalSourceFileID = FileID::get(
4310 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4311 }
4312
4313 // Preload all the pending interesting identifiers by marking them out of
4314 // date.
4315 for (auto Offset : F.PreloadIdentifierOffsets) {
4316 const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4317 F.IdentifierTableData + Offset);
4318
4319 ASTIdentifierLookupTrait Trait(*this, F);
4320 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4321 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4322 auto &II = PP.getIdentifierTable().getOwn(Key);
4323 II.setOutOfDate(true);
4324
4325 // Mark this identifier as being from an AST file so that we can track
4326 // whether we need to serialize it.
4327 markIdentifierFromAST(*this, II);
4328
4329 // Associate the ID with the identifier so that the writer can reuse it.
4330 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4331 SetIdentifierInfo(ID, &II);
4332 }
4333 }
4334
4335 // Setup the import locations and notify the module manager that we've
4336 // committed to these module files.
4337 for (ImportedModule &M : Loaded) {
4338 ModuleFile &F = *M.Mod;
4339
4340 ModuleMgr.moduleFileAccepted(&F);
4341
4342 // Set the import location.
4343 F.DirectImportLoc = ImportLoc;
4344 // FIXME: We assume that locations from PCH / preamble do not need
4345 // any translation.
4346 if (!M.ImportedBy)
4347 F.ImportLoc = M.ImportLoc;
4348 else
4349 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4350 }
4351
4352 if (!PP.getLangOpts().CPlusPlus ||
4353 (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4354 Type != MK_PrebuiltModule)) {
4355 // Mark all of the identifiers in the identifier table as being out of date,
4356 // so that various accessors know to check the loaded modules when the
4357 // identifier is used.
4358 //
4359 // For C++ modules, we don't need information on many identifiers (just
4360 // those that provide macros or are poisoned), so we mark all of
4361 // the interesting ones via PreloadIdentifierOffsets.
4362 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4363 IdEnd = PP.getIdentifierTable().end();
4364 Id != IdEnd; ++Id)
4365 Id->second->setOutOfDate(true);
4366 }
4367 // Mark selectors as out of date.
4368 for (auto Sel : SelectorGeneration)
4369 SelectorOutOfDate[Sel.first] = true;
4370
4371 // Resolve any unresolved module exports.
4372 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4373 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4374 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4375 Module *ResolvedMod = getSubmodule(GlobalID);
4376
4377 switch (Unresolved.Kind) {
4378 case UnresolvedModuleRef::Conflict:
4379 if (ResolvedMod) {
4380 Module::Conflict Conflict;
4381 Conflict.Other = ResolvedMod;
4382 Conflict.Message = Unresolved.String.str();
4383 Unresolved.Mod->Conflicts.push_back(Conflict);
4384 }
4385 continue;
4386
4387 case UnresolvedModuleRef::Import:
4388 if (ResolvedMod)
4389 Unresolved.Mod->Imports.insert(ResolvedMod);
4390 continue;
4391
4392 case UnresolvedModuleRef::Export:
4393 if (ResolvedMod || Unresolved.IsWildcard)
4394 Unresolved.Mod->Exports.push_back(
4395 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4396 continue;
4397 }
4398 }
4399 UnresolvedModuleRefs.clear();
4400
4401 if (Imported)
4402 Imported->append(ImportedModules.begin(),
4403 ImportedModules.end());
4404
4405 // FIXME: How do we load the 'use'd modules? They may not be submodules.
4406 // Might be unnecessary as use declarations are only used to build the
4407 // module itself.
4408
4409 if (ContextObj)
4410 InitializeContext();
4411
4412 if (SemaObj)
4413 UpdateSema();
4414
4415 if (DeserializationListener)
4416 DeserializationListener->ReaderInitialized(this);
4417
4418 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4419 if (PrimaryModule.OriginalSourceFileID.isValid()) {
4420 // If this AST file is a precompiled preamble, then set the
4421 // preamble file ID of the source manager to the file source file
4422 // from which the preamble was built.
4423 if (Type == MK_Preamble) {
4424 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4425 } else if (Type == MK_MainFile) {
4426 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4427 }
4428 }
4429
4430 // For any Objective-C class definitions we have already loaded, make sure
4431 // that we load any additional categories.
4432 if (ContextObj) {
4433 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4434 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4435 ObjCClassesLoaded[I],
4436 PreviousGeneration);
4437 }
4438 }
4439
4440 if (PP.getHeaderSearchInfo()
4441 .getHeaderSearchOpts()
4442 .ModulesValidateOncePerBuildSession) {
4443 // Now we are certain that the module and all modules it depends on are
4444 // up to date. Create or update timestamp files for modules that are
4445 // located in the module cache (not for PCH files that could be anywhere
4446 // in the filesystem).
4447 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4448 ImportedModule &M = Loaded[I];
4449 if (M.Mod->Kind == MK_ImplicitModule) {
4450 updateModuleTimestamp(*M.Mod);
4451 }
4452 }
4453 }
4454
4455 return Success;
4456}
4457
4458static ASTFileSignature readASTFileSignature(StringRef PCH);
4459
4460/// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4461static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4462 // FIXME checking magic headers is done in other places such as
4463 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4464 // always done the same. Unify it all with a helper.
4465 if (!Stream.canSkipToPos(4))
4466 return llvm::createStringError(std::errc::illegal_byte_sequence,
4467 "file too small to contain AST file magic");
4468 for (unsigned C : {'C', 'P', 'C', 'H'})
4469 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4470 if (Res.get() != C)
4471 return llvm::createStringError(
4472 std::errc::illegal_byte_sequence,
4473 "file doesn't start with AST file magic");
4474 } else
4475 return Res.takeError();
4476 return llvm::Error::success();
4477}
4478
4479static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4480 switch (Kind) {
4481 case MK_PCH:
4482 return 0; // PCH
4483 case MK_ImplicitModule:
4484 case MK_ExplicitModule:
4485 case MK_PrebuiltModule:
4486 return 1; // module
4487 case MK_MainFile:
4488 case MK_Preamble:
4489 return 2; // main source file
4490 }
4491 llvm_unreachable("unknown module kind");
4492}
4493
4494ASTReader::ASTReadResult
4495ASTReader::ReadASTCore(StringRef FileName,
4496 ModuleKind Type,
4497 SourceLocation ImportLoc,
4498 ModuleFile *ImportedBy,
4499 SmallVectorImpl<ImportedModule> &Loaded,
4500 off_t ExpectedSize, time_t ExpectedModTime,
4501 ASTFileSignature ExpectedSignature,
4502 unsigned ClientLoadCapabilities) {
4503 ModuleFile *M;
4504 std::string ErrorStr;
4505 ModuleManager::AddModuleResult AddResult
4506 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4507 getGeneration(), ExpectedSize, ExpectedModTime,
4508 ExpectedSignature, readASTFileSignature,
4509 M, ErrorStr);
4510
4511 switch (AddResult) {
4512 case ModuleManager::AlreadyLoaded:
4513 Diag(diag::remark_module_import)
4514 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4515 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4516 return Success;
4517
4518 case ModuleManager::NewlyLoaded:
4519 // Load module file below.
4520 break;
4521
4522 case ModuleManager::Missing:
4523 // The module file was missing; if the client can handle that, return
4524 // it.
4525 if (ClientLoadCapabilities & ARR_Missing)
4526 return Missing;
4527
4528 // Otherwise, return an error.
4529 Diag(diag::err_ast_file_not_found)
4530 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4531 << ErrorStr;
4532 return Failure;
4533
4534 case ModuleManager::OutOfDate:
4535 // We couldn't load the module file because it is out-of-date. If the
4536 // client can handle out-of-date, return it.
4537 if (ClientLoadCapabilities & ARR_OutOfDate)
4538 return OutOfDate;
4539
4540 // Otherwise, return an error.
4541 Diag(diag::err_ast_file_out_of_date)
4542 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4543 << ErrorStr;
4544 return Failure;
4545 }
4546
4547 assert(M && "Missing module file");
4548
4549 bool ShouldFinalizePCM = false;
4550 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4551 auto &MC = getModuleManager().getModuleCache();
4552 if (ShouldFinalizePCM)
4553 MC.finalizePCM(FileName);
4554 else
4555 MC.tryToDropPCM(FileName);
4556 });
4557 ModuleFile &F = *M;
4558 BitstreamCursor &Stream = F.Stream;
4559 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4560 F.SizeInBits = F.Buffer->getBufferSize() * 8;
4561
4562 // Sniff for the signature.
4563 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4564 Diag(diag::err_ast_file_invalid)
4565 << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4566 return Failure;
4567 }
4568
4569 // This is used for compatibility with older PCH formats.
4570 bool HaveReadControlBlock = false;
4571 while (true) {
4572 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4573 if (!MaybeEntry) {
4574 Error(MaybeEntry.takeError());
4575 return Failure;
4576 }
4577 llvm::BitstreamEntry Entry = MaybeEntry.get();
4578
4579 switch (Entry.Kind) {
4580 case llvm::BitstreamEntry::Error:
4581 case llvm::BitstreamEntry::Record:
4582 case llvm::BitstreamEntry::EndBlock:
4583 Error("invalid record at top-level of AST file");
4584 return Failure;
4585
4586 case llvm::BitstreamEntry::SubBlock:
4587 break;
4588 }
4589
4590 switch (Entry.ID) {
4591 case CONTROL_BLOCK_ID:
4592 HaveReadControlBlock = true;
4593 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4594 case Success:
4595 // Check that we didn't try to load a non-module AST file as a module.
4596 //
4597 // FIXME: Should we also perform the converse check? Loading a module as
4598 // a PCH file sort of works, but it's a bit wonky.
4599 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4600 Type == MK_PrebuiltModule) &&
4601 F.ModuleName.empty()) {
4602 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4603 if (Result != OutOfDate ||
4604 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4605 Diag(diag::err_module_file_not_module) << FileName;
4606 return Result;
4607 }
4608 break;
4609
4610 case Failure: return Failure;
4611 case Missing: return Missing;
4612 case OutOfDate: return OutOfDate;
4613 case VersionMismatch: return VersionMismatch;
4614 case ConfigurationMismatch: return ConfigurationMismatch;
4615 case HadErrors: return HadErrors;
4616 }
4617 break;
4618
4619 case AST_BLOCK_ID:
4620 if (!HaveReadControlBlock) {
4621 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4622 Diag(diag::err_pch_version_too_old);
4623 return VersionMismatch;
4624 }
4625
4626 // Record that we've loaded this module.
4627 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4628 ShouldFinalizePCM = true;
4629 return Success;
4630
4631 case UNHASHED_CONTROL_BLOCK_ID:
4632 // This block is handled using look-ahead during ReadControlBlock. We
4633 // shouldn't get here!
4634 Error("malformed block record in AST file");
4635 return Failure;
4636
4637 default:
4638 if (llvm::Error Err = Stream.SkipBlock()) {
4639 Error(std::move(Err));
4640 return Failure;
4641 }
4642 break;
4643 }
4644 }
4645
4646 llvm_unreachable("unexpected break; expected return");
4647}
4648
4649ASTReader::ASTReadResult
4650ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4651 unsigned ClientLoadCapabilities) {
4652 const HeaderSearchOptions &HSOpts =
4653 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4654 bool AllowCompatibleConfigurationMismatch =
4655 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4656 bool DisableValidation = shouldDisableValidationForFile(F);
4657
4658 ASTReadResult Result = readUnhashedControlBlockImpl(
4659 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4660 Listener.get(),
4661 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4662
4663 // If F was directly imported by another module, it's implicitly validated by
4664 // the importing module.
4665 if (DisableValidation || WasImportedBy ||
4666 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4667 return Success;
4668
4669 if (Result == Failure) {
4670 Error("malformed block record in AST file");
4671 return Failure;
4672 }
4673
4674 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4675 // If this module has already been finalized in the ModuleCache, we're stuck
4676 // with it; we can only load a single version of each module.
4677 //
4678 // This can happen when a module is imported in two contexts: in one, as a
4679 // user module; in another, as a system module (due to an import from
4680 // another module marked with the [system] flag). It usually indicates a
4681 // bug in the module map: this module should also be marked with [system].
4682 //
4683 // If -Wno-system-headers (the default), and the first import is as a
4684 // system module, then validation will fail during the as-user import,
4685 // since -Werror flags won't have been validated. However, it's reasonable
4686 // to treat this consistently as a system module.
4687 //
4688 // If -Wsystem-headers, the PCM on disk was built with
4689 // -Wno-system-headers, and the first import is as a user module, then
4690 // validation will fail during the as-system import since the PCM on disk
4691 // doesn't guarantee that -Werror was respected. However, the -Werror
4692 // flags were checked during the initial as-user import.
4693 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4694 Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4695 return Success;
4696 }
4697 }
4698
4699 return Result;
4700}
4701
4702ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4703 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4704 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4705 bool ValidateDiagnosticOptions) {
4706 // Initialize a stream.
4707 BitstreamCursor Stream(StreamData);
4708
4709 // Sniff for the signature.
4710 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4711 // FIXME this drops the error on the floor.
4712 consumeError(std::move(Err));
4713 return Failure;
4714 }
4715
4716 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4717 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4718 return Failure;
4719
4720 // Read all of the records in the options block.
4721 RecordData Record;
4722 ASTReadResult Result = Success;
4723 while (true) {
4724 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4725 if (!MaybeEntry) {
4726 // FIXME this drops the error on the floor.
4727 consumeError(MaybeEntry.takeError());
4728 return Failure;
4729 }
4730 llvm::BitstreamEntry Entry = MaybeEntry.get();
4731
4732 switch (Entry.Kind) {
4733 case llvm::BitstreamEntry::Error:
4734 case llvm::BitstreamEntry::SubBlock:
4735 return Failure;
4736
4737 case llvm::BitstreamEntry::EndBlock:
4738 return Result;
4739
4740 case llvm::BitstreamEntry::Record:
4741 // The interesting case.
4742 break;
4743 }
4744
4745 // Read and process a record.
4746 Record.clear();
4747 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4748 if (!MaybeRecordType) {
4749 // FIXME this drops the error.
4750 return Failure;
4751 }
4752 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4753 case SIGNATURE:
4754 if (F)
4755 F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4756 break;
4757 case AST_BLOCK_HASH:
4758 if (F)
4759 F->ASTBlockHash =
4760 ASTFileSignature::create(Record.begin(), Record.end());
4761 break;
4762 case DIAGNOSTIC_OPTIONS: {
4763 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4764 if (Listener && ValidateDiagnosticOptions &&
4765 !AllowCompatibleConfigurationMismatch &&
4766 ParseDiagnosticOptions(Record, Complain, *Listener))
4767 Result = OutOfDate; // Don't return early. Read the signature.
4768 break;
4769 }
4770 case DIAG_PRAGMA_MAPPINGS:
4771 if (!F)
4772 break;
4773 if (F->PragmaDiagMappings.empty())
4774 F->PragmaDiagMappings.swap(Record);
4775 else
4776 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4777 Record.begin(), Record.end());
4778 break;
4779 }
4780 }
4781}
4782
4783/// Parse a record and blob containing module file extension metadata.
4784static bool parseModuleFileExtensionMetadata(
4785 const SmallVectorImpl<uint64_t> &Record,
4786 StringRef Blob,
4787 ModuleFileExtensionMetadata &Metadata) {
4788 if (Record.size() < 4) return true;
4789
4790 Metadata.MajorVersion = Record[0];
4791 Metadata.MinorVersion = Record[1];
4792
4793 unsigned BlockNameLen = Record[2];
4794 unsigned UserInfoLen = Record[3];
4795
4796 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4797
4798 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4799 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4800 Blob.data() + BlockNameLen + UserInfoLen);
4801 return false;
4802}
4803
4804ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4805 BitstreamCursor &Stream = F.Stream;
4806
4807 RecordData Record;
4808 while (true) {
4809 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4810 if (!MaybeEntry) {
4811 Error(MaybeEntry.takeError());
4812 return Failure;
4813 }
4814 llvm::BitstreamEntry Entry = MaybeEntry.get();
4815
4816 switch (Entry.Kind) {
4817 case llvm::BitstreamEntry::SubBlock:
4818 if (llvm::Error Err = Stream.SkipBlock()) {
4819 Error(std::move(Err));
4820 return Failure;
4821 }
4822 continue;
4823
4824 case llvm::BitstreamEntry::EndBlock:
4825 return Success;
4826
4827 case llvm::BitstreamEntry::Error:
4828 return HadErrors;
4829
4830 case llvm::BitstreamEntry::Record:
4831 break;
4832 }
4833
4834 Record.clear();
4835 StringRef Blob;
4836 Expected<unsigned> MaybeRecCode =
4837 Stream.readRecord(Entry.ID, Record, &Blob);
4838 if (!MaybeRecCode) {
4839 Error(MaybeRecCode.takeError());
4840 return Failure;
4841 }
4842 switch (MaybeRecCode.get()) {
4843 case EXTENSION_METADATA: {
4844 ModuleFileExtensionMetadata Metadata;
4845 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4846 Error("malformed EXTENSION_METADATA in AST file");
4847 return Failure;
4848 }
4849
4850 // Find a module file extension with this block name.
4851 auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4852 if (Known == ModuleFileExtensions.end()) break;
4853
4854 // Form a reader.
4855 if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4856 F, Stream)) {
4857 F.ExtensionReaders.push_back(std::move(Reader));
4858 }
4859
4860 break;
4861 }
4862 }
4863 }
4864
4865 return Success;
4866}
4867
4868void ASTReader::InitializeContext() {
4869 assert(ContextObj && "no context to initialize");
4870 ASTContext &Context = *ContextObj;
4871
4872 // If there's a listener, notify them that we "read" the translation unit.
4873 if (DeserializationListener)
4874 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4875 Context.getTranslationUnitDecl());
4876
4877 // FIXME: Find a better way to deal with collisions between these
4878 // built-in types. Right now, we just ignore the problem.
4879
4880 // Load the special types.
4881 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4882 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4883 if (!Context.CFConstantStringTypeDecl)
4884 Context.setCFConstantStringType(GetType(String));
4885 }
4886
4887 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4888 QualType FileType = GetType(File);
4889 if (FileType.isNull()) {
4890 Error("FILE type is NULL");
4891 return;
4892 }
4893
4894 if (!Context.FILEDecl) {
4895 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4896 Context.setFILEDecl(Typedef->getDecl());
4897 else {
4898 const TagType *Tag = FileType->getAs<TagType>();
4899 if (!Tag) {
4900 Error("Invalid FILE type in AST file");
4901 return;
4902 }
4903 Context.setFILEDecl(Tag->getDecl());
4904 }
4905 }
4906 }
4907
4908 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4909 QualType Jmp_bufType = GetType(Jmp_buf);
4910 if (Jmp_bufType.isNull()) {
4911 Error("jmp_buf type is NULL");
4912 return;
4913 }
4914
4915 if (!Context.jmp_bufDecl) {
4916 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4917 Context.setjmp_bufDecl(Typedef->getDecl());
4918 else {
4919 const TagType *Tag = Jmp_bufType->getAs<TagType>();
4920 if (!Tag) {
4921 Error("Invalid jmp_buf type in AST file");
4922 return;
4923 }
4924 Context.setjmp_bufDecl(Tag->getDecl());
4925 }
4926 }
4927 }
4928
4929 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4930 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4931 if (Sigjmp_bufType.isNull()) {
4932 Error("sigjmp_buf type is NULL");
4933 return;
4934 }
4935
4936 if (!Context.sigjmp_bufDecl) {
4937 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4938 Context.setsigjmp_bufDecl(Typedef->getDecl());
4939 else {
4940 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4941 assert(Tag && "Invalid sigjmp_buf type in AST file");
4942 Context.setsigjmp_bufDecl(Tag->getDecl());
4943 }
4944 }
4945 }
4946
4947 if (unsigned ObjCIdRedef
4948 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4949 if (Context.ObjCIdRedefinitionType.isNull())
4950 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4951 }
4952
4953 if (unsigned ObjCClassRedef
4954 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4955 if (Context.ObjCClassRedefinitionType.isNull())
4956 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4957 }
4958
4959 if (unsigned ObjCSelRedef
4960 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4961 if (Context.ObjCSelRedefinitionType.isNull())
4962 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4963 }
4964
4965 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4966 QualType Ucontext_tType = GetType(Ucontext_t);
4967 if (Ucontext_tType.isNull()) {
4968 Error("ucontext_t type is NULL");
4969 return;
4970 }
4971
4972 if (!Context.ucontext_tDecl) {
4973 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4974 Context.setucontext_tDecl(Typedef->getDecl());
4975 else {
4976 const TagType *Tag = Ucontext_tType->getAs<TagType>();
4977 assert(Tag && "Invalid ucontext_t type in AST file");
4978 Context.setucontext_tDecl(Tag->getDecl());
4979 }
4980 }
4981 }
4982 }
4983
4984 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4985
4986 // If there were any CUDA special declarations, deserialize them.
4987 if (!CUDASpecialDeclRefs.empty()) {
4988 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4989 Context.setcudaConfigureCallDecl(
4990 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4991 }
4992
4993 // Re-export any modules that were imported by a non-module AST file.
4994 // FIXME: This does not make macro-only imports visible again.
4995 for (auto &Import : ImportedModules) {
4996 if (Module *Imported = getSubmodule(Import.ID)) {
4997 makeModuleVisible(Imported, Module::AllVisible,
4998 /*ImportLoc=*/Import.ImportLoc);
4999 if (Import.ImportLoc.isValid())
5000 PP.makeModuleVisible(Imported, Import.ImportLoc);
5001 // This updates visibility for Preprocessor only. For Sema, which can be
5002 // nullptr here, we do the same later, in UpdateSema().
5003 }
5004 }
5005}
5006
5007void ASTReader::finalizeForWriting() {
5008 // Nothing to do for now.
5009}
5010
5011/// Reads and return the signature record from \p PCH's control block, or
5012/// else returns 0.
5013static ASTFileSignature readASTFileSignature(StringRef PCH) {
5014 BitstreamCursor Stream(PCH);
5015 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5016 // FIXME this drops the error on the floor.
5017 consumeError(std::move(Err));
5018 return ASTFileSignature();
5019 }
5020
5021 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5022 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5023 return ASTFileSignature();
5024
5025 // Scan for SIGNATURE inside the diagnostic options block.
5026 ASTReader::RecordData Record;
5027 while (true) {
5028 Expected<llvm::BitstreamEntry> MaybeEntry =
5029 Stream.advanceSkippingSubblocks();
5030 if (!MaybeEntry) {
5031 // FIXME this drops the error on the floor.
5032 consumeError(MaybeEntry.takeError());
5033 return ASTFileSignature();
5034 }
5035 llvm::BitstreamEntry Entry = MaybeEntry.get();
5036
5037 if (Entry.Kind != llvm::BitstreamEntry::Record)
5038 return ASTFileSignature();
5039
5040 Record.clear();
5041 StringRef Blob;
5042 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5043 if (!MaybeRecord) {
5044 // FIXME this drops the error on the floor.
5045 consumeError(MaybeRecord.takeError());
5046 return ASTFileSignature();
5047 }
5048 if (SIGNATURE == MaybeRecord.get())
5049 return ASTFileSignature::create(Record.begin(),
5050 Record.begin() + ASTFileSignature::size);
5051 }
5052}
5053
5054/// Retrieve the name of the original source file name
5055/// directly from the AST file, without actually loading the AST
5056/// file.
5057std::string ASTReader::getOriginalSourceFile(
5058 const std::string &ASTFileName, FileManager &FileMgr,
5059 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5060 // Open the AST file.
5061 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5062 if (!Buffer) {
5063 Diags.Report(diag::err_fe_unable_to_read_pch_file)
5064 << ASTFileName << Buffer.getError().message();
5065 return std::string();
5066 }
5067
5068 // Initialize the stream
5069 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5070
5071 // Sniff for the signature.
5072 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5073 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5074 return std::string();
5075 }
5076
5077 // Scan for the CONTROL_BLOCK_ID block.
5078 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5079 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5080 return std::string();
5081 }
5082
5083 // Scan for ORIGINAL_FILE inside the control block.
5084 RecordData Record;
5085 while (true) {
5086 Expected<llvm::BitstreamEntry> MaybeEntry =
5087 Stream.advanceSkippingSubblocks();
5088 if (!MaybeEntry) {
5089 // FIXME this drops errors on the floor.
5090 consumeError(MaybeEntry.takeError());
5091 return std::string();
5092 }
5093 llvm::BitstreamEntry Entry = MaybeEntry.get();
5094
5095 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5096 return std::string();
5097
5098 if (Entry.Kind != llvm::BitstreamEntry::Record) {
5099 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5100 return std::string();
5101 }
5102
5103 Record.clear();
5104 StringRef Blob;
5105 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5106 if (!MaybeRecord) {
5107 // FIXME this drops the errors on the floor.
5108 consumeError(MaybeRecord.takeError());
5109 return std::string();
5110 }
5111 if (ORIGINAL_FILE == MaybeRecord.get())
5112 return Blob.str();
5113 }
5114}
5115
5116namespace {
5117
5118 class SimplePCHValidator : public ASTReaderListener {
5119 const LangOptions &ExistingLangOpts;
5120 const TargetOptions &ExistingTargetOpts;
5121 const PreprocessorOptions &ExistingPPOpts;
5122 std::string ExistingModuleCachePath;
5123 FileManager &FileMgr;
5124
5125 public:
5126 SimplePCHValidator(const LangOptions &ExistingLangOpts,
5127 const TargetOptions &ExistingTargetOpts,
5128 const PreprocessorOptions &ExistingPPOpts,
5129 StringRef ExistingModuleCachePath, FileManager &FileMgr)
5130 : ExistingLangOpts(ExistingLangOpts),
5131 ExistingTargetOpts(ExistingTargetOpts),
5132 ExistingPPOpts(ExistingPPOpts),
5133 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5134
5135 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5136 bool AllowCompatibleDifferences) override {
5137 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5138 AllowCompatibleDifferences);
5139 }
5140
5141 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5142 bool AllowCompatibleDifferences) override {
5143 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5144 AllowCompatibleDifferences);
5145 }
5146
5147 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5148 StringRef SpecificModuleCachePath,
5149 bool Complain) override {
5150 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5151 ExistingModuleCachePath,
5152 nullptr, ExistingLangOpts);
5153 }
5154
5155 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5156 bool Complain,
5157 std::string &SuggestedPredefines) override {
5158 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5159 SuggestedPredefines, ExistingLangOpts);
5160 }
5161 };
5162
5163} // namespace
5164
5165bool ASTReader::readASTFileControlBlock(
5166 StringRef Filename, FileManager &FileMgr,
5167 const PCHContainerReader &PCHContainerRdr,
5168 bool FindModuleFileExtensions,
5169 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5170 // Open the AST file.
5171 // FIXME: This allows use of the VFS; we do not allow use of the
5172 // VFS when actually loading a module.
5173 auto Buffer = FileMgr.getBufferForFile(Filename);
5174 if (!Buffer) {
5175 return true;
5176 }
5177
5178 // Initialize the stream
5179 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5180 BitstreamCursor Stream(Bytes);
5181
5182 // Sniff for the signature.
5183 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5184 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5185 return true;
5186 }
5187
5188 // Scan for the CONTROL_BLOCK_ID block.
5189 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5190 return true;
5191
5192 bool NeedsInputFiles = Listener.needsInputFileVisitation();
5193 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5194 bool NeedsImports = Listener.needsImportVisitation();
5195 BitstreamCursor InputFilesCursor;
5196
5197 RecordData Record;
5198 std::string ModuleDir;
5199 bool DoneWithControlBlock = false;
5200 while (!DoneWithControlBlock) {
5201 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5202 if (!MaybeEntry) {
5203 // FIXME this drops the error on the floor.
5204 consumeError(MaybeEntry.takeError());
5205 return true;
5206 }
5207 llvm::BitstreamEntry Entry = MaybeEntry.get();
5208
5209 switch (Entry.Kind) {
5210 case llvm::BitstreamEntry::SubBlock: {
5211 switch (Entry.ID) {
5212 case OPTIONS_BLOCK_ID: {
5213 std::string IgnoredSuggestedPredefines;
5214 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5215 /*AllowCompatibleConfigurationMismatch*/ false,
5216 Listener, IgnoredSuggestedPredefines) != Success)
5217 return true;
5218 break;
5219 }
5220
5221 case INPUT_FILES_BLOCK_ID:
5222 InputFilesCursor = Stream;
5223 if (llvm::Error Err = Stream.SkipBlock()) {
5224 // FIXME this drops the error on the floor.
5225 consumeError(std::move(Err));
5226 return true;
5227 }
5228 if (NeedsInputFiles &&
5229 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5230 return true;
5231 break;
5232
5233 default:
5234 if (llvm::Error Err = Stream.SkipBlock()) {
5235 // FIXME this drops the error on the floor.
5236 consumeError(std::move(Err));
5237 return true;
5238 }
5239 break;
5240 }
5241
5242 continue;
5243 }
5244
5245 case llvm::BitstreamEntry::EndBlock:
5246 DoneWithControlBlock = true;
5247 break;
5248
5249 case llvm::BitstreamEntry::Error:
5250 return true;
5251
5252 case llvm::BitstreamEntry::Record:
5253 break;
5254 }
5255
5256 if (DoneWithControlBlock) break;
5257
5258 Record.clear();
5259 StringRef Blob;
5260 Expected<unsigned> MaybeRecCode =
5261 Stream.readRecord(Entry.ID, Record, &Blob);
5262 if (!MaybeRecCode) {
5263 // FIXME this drops the error.
5264 return Failure;
5265 }
5266 switch ((ControlRecordTypes)MaybeRecCode.get()) {
5267 case METADATA:
5268 if (Record[0] != VERSION_MAJOR)
5269 return true;
5270 if (Listener.ReadFullVersionInformation(Blob))
5271 return true;
5272 break;
5273 case MODULE_NAME:
5274 Listener.ReadModuleName(Blob);
5275 break;
5276 case MODULE_DIRECTORY:
5277 ModuleDir = std::string(Blob);
5278 break;
5279 case MODULE_MAP_FILE: {
5280 unsigned Idx = 0;
5281 auto Path = ReadString(Record, Idx);
5282 ResolveImportedPath(Path, ModuleDir);
5283 Listener.ReadModuleMapFile(Path);
5284 break;
5285 }
5286 case INPUT_FILE_OFFSETS: {
5287 if (!NeedsInputFiles)
5288 break;
5289
5290 unsigned NumInputFiles = Record[0];
5291 unsigned NumUserFiles = Record[1];
5292 const llvm::support::unaligned_uint64_t *InputFileOffs =
5293 (const llvm::support::unaligned_uint64_t *)Blob.data();
5294 for (unsigned I = 0; I != NumInputFiles; ++I) {
5295 // Go find this input file.
5296 bool isSystemFile = I >= NumUserFiles;
5297
5298 if (isSystemFile && !NeedsSystemInputFiles)
5299 break; // the rest are system input files
5300
5301 BitstreamCursor &Cursor = InputFilesCursor;
5302 SavedStreamPosition SavedPosition(Cursor);
5303 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5304 // FIXME this drops errors on the floor.
5305 consumeError(std::move(Err));
5306 }
5307
5308 Expected<unsigned> MaybeCode = Cursor.ReadCode();
5309 if (!MaybeCode) {
5310 // FIXME this drops errors on the floor.
5311 consumeError(MaybeCode.takeError());
5312 }
5313 unsigned Code = MaybeCode.get();
5314
5315 RecordData Record;
5316 StringRef Blob;
5317 bool shouldContinue = false;
5318 Expected<unsigned> MaybeRecordType =
5319 Cursor.readRecord(Code, Record, &Blob);
5320 if (!MaybeRecordType) {
5321 // FIXME this drops errors on the floor.
5322 consumeError(MaybeRecordType.takeError());
5323 }
5324 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5325 case INPUT_FILE_HASH:
5326 break;
5327 case INPUT_FILE:
5328 bool Overridden = static_cast<bool>(Record[3]);
5329 std::string Filename = std::string(Blob);
5330 ResolveImportedPath(Filename, ModuleDir);
5331 shouldContinue = Listener.visitInputFile(
5332 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5333 break;
5334 }
5335 if (!shouldContinue)
5336 break;
5337 }
5338 break;
5339 }
5340
5341 case IMPORTS: {
5342 if (!NeedsImports)
5343 break;
5344
5345 unsigned Idx = 0, N = Record.size();
5346 while (Idx < N) {
5347 // Read information about the AST file.
5348 Idx +=
5349 1 + 1 + 1 + 1 +
5350 ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5351 std::string ModuleName = ReadString(Record, Idx);
5352 std::string Filename = ReadString(Record, Idx);
5353 ResolveImportedPath(Filename, ModuleDir);
5354 Listener.visitImport(ModuleName, Filename);
5355 }
5356 break;
5357 }
5358
5359 default:
5360 // No other validation to perform.
5361 break;
5362 }
5363 }
5364
5365 // Look for module file extension blocks, if requested.
5366 if (FindModuleFileExtensions) {
5367 BitstreamCursor SavedStream = Stream;
5368 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5369 bool DoneWithExtensionBlock = false;
5370 while (!DoneWithExtensionBlock) {
5371 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5372 if (!MaybeEntry) {
5373 // FIXME this drops the error.
5374 return true;
5375 }
5376 llvm::BitstreamEntry Entry = MaybeEntry.get();
5377
5378 switch (Entry.Kind) {
5379 case llvm::BitstreamEntry::SubBlock:
5380 if (llvm::Error Err = Stream.SkipBlock()) {
5381 // FIXME this drops the error on the floor.
5382 consumeError(std::move(Err));
5383 return true;
5384 }
5385 continue;
5386
5387 case llvm::BitstreamEntry::EndBlock:
5388 DoneWithExtensionBlock = true;
5389 continue;
5390
5391 case llvm::BitstreamEntry::Error:
5392 return true;
5393
5394 case llvm::BitstreamEntry::Record:
5395 break;
5396 }
5397
5398 Record.clear();
5399 StringRef Blob;
5400 Expected<unsigned> MaybeRecCode =
5401 Stream.readRecord(Entry.ID, Record, &Blob);
5402 if (!MaybeRecCode) {
5403 // FIXME this drops the error.
5404 return true;
5405 }
5406 switch (MaybeRecCode.get()) {
5407 case EXTENSION_METADATA: {
5408 ModuleFileExtensionMetadata Metadata;
5409 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5410 return true;
5411
5412 Listener.readModuleFileExtension(Metadata);
5413 break;
5414 }
5415 }
5416 }
5417 }
5418 Stream = SavedStream;
5419 }
5420
5421 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5422 if (readUnhashedControlBlockImpl(
5423 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5424 /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5425 ValidateDiagnosticOptions) != Success)
5426 return true;
5427
5428 return false;
5429}
5430
5431bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5432 const PCHContainerReader &PCHContainerRdr,
5433 const LangOptions &LangOpts,
5434 const TargetOptions &TargetOpts,
5435 const PreprocessorOptions &PPOpts,
5436 StringRef ExistingModuleCachePath) {
5437 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5438 ExistingModuleCachePath, FileMgr);
5439 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5440 /*FindModuleFileExtensions=*/false,
5441 validator,
5442 /*ValidateDiagnosticOptions=*/true);
5443}
5444
5445ASTReader::ASTReadResult
5446ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5447 // Enter the submodule block.
5448 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5449 Error(std::move(Err));
5450 return Failure;
5451 }
5452
5453 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5454 bool First = true;
5455 Module *CurrentModule = nullptr;
5456 RecordData Record;
5457 while (true) {
5458 Expected<llvm::BitstreamEntry> MaybeEntry =
5459 F.Stream.advanceSkippingSubblocks();
5460 if (!MaybeEntry) {
5461 Error(MaybeEntry.takeError());
5462 return Failure;
5463 }
5464 llvm::BitstreamEntry Entry = MaybeEntry.get();
5465
5466 switch (Entry.Kind) {
5467 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5468 case llvm::BitstreamEntry::Error:
5469 Error("malformed block record in AST file");
5470 return Failure;
5471 case llvm::BitstreamEntry::EndBlock:
5472 return Success;
5473 case llvm::BitstreamEntry::Record:
5474 // The interesting case.
5475 break;
5476 }
5477
5478 // Read a record.
5479 StringRef Blob;
5480 Record.clear();
5481 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5482 if (!MaybeKind) {
5483 Error(MaybeKind.takeError());
5484 return Failure;
5485 }
5486 unsigned Kind = MaybeKind.get();
5487
5488 if ((Kind == SUBMODULE_METADATA) != First) {
5489 Error("submodule metadata record should be at beginning of block");
5490 return Failure;
5491 }
5492 First = false;
5493
5494 // Submodule information is only valid if we have a current module.
5495 // FIXME: Should we error on these cases?
5496 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5497 Kind != SUBMODULE_DEFINITION)
5498 continue;
5499
5500 switch (Kind) {
5501 default: // Default behavior: ignore.
5502 break;
5503
5504 case SUBMODULE_DEFINITION: {
5505 if (Record.size() < 12) {
5506 Error("malformed module definition");
5507 return Failure;
5508 }
5509
5510 StringRef Name = Blob;
5511 unsigned Idx = 0;
5512 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5513 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5514 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5515 bool IsFramework = Record[Idx++];
5516 bool IsExplicit = Record[Idx++];
5517 bool IsSystem = Record[Idx++];
5518 bool IsExternC = Record[Idx++];
5519 bool InferSubmodules = Record[Idx++];
5520 bool InferExplicitSubmodules = Record[Idx++];
5521 bool InferExportWildcard = Record[Idx++];
5522 bool ConfigMacrosExhaustive = Record[Idx++];
5523 bool ModuleMapIsPrivate = Record[Idx++];
5524
5525 Module *ParentModule = nullptr;
5526 if (Parent)
5527 ParentModule = getSubmodule(Parent);
5528
5529 // Retrieve this (sub)module from the module map, creating it if
5530 // necessary.
5531 CurrentModule =
5532 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5533 .first;
5534
5535 // FIXME: set the definition loc for CurrentModule, or call
5536 // ModMap.setInferredModuleAllowedBy()
5537
5538 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5539 if (GlobalIndex >= SubmodulesLoaded.size() ||
5540 SubmodulesLoaded[GlobalIndex]) {
5541 Error("too many submodules");
5542 return Failure;
5543 }
5544
5545 if (!ParentModule) {
5546 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5547 // Don't emit module relocation error if we have -fno-validate-pch
5548 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
5549 DisableValidationForModuleKind::Module) &&
5550 CurFile != F.File) {
5551 Error(diag::err_module_file_conflict,
5552 CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5553 F.File->getName());
5554 return Failure;
5555 }
5556 }
5557
5558 F.DidReadTopLevelSubmodule = true;
5559 CurrentModule->setASTFile(F.File);
5560 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5561 }
5562
5563 CurrentModule->Kind = Kind;
5564 CurrentModule->Signature = F.Signature;
5565 CurrentModule->IsFromModuleFile = true;
5566 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5567 CurrentModule->IsExternC = IsExternC;
5568 CurrentModule->InferSubmodules = InferSubmodules;
5569 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5570 CurrentModule->InferExportWildcard = InferExportWildcard;
5571 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5572 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5573 if (DeserializationListener)
5574 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5575
5576 SubmodulesLoaded[GlobalIndex] = CurrentModule;
5577
5578 // Clear out data that will be replaced by what is in the module file.
5579 CurrentModule->LinkLibraries.clear();
5580 CurrentModule->ConfigMacros.clear();
5581 CurrentModule->UnresolvedConflicts.clear();
5582 CurrentModule->Conflicts.clear();
5583
5584 // The module is available unless it's missing a requirement; relevant
5585 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5586 // Missing headers that were present when the module was built do not
5587 // make it unavailable -- if we got this far, this must be an explicitly
5588 // imported module file.
5589 CurrentModule->Requirements.clear();
5590 CurrentModule->MissingHeaders.clear();
5591 CurrentModule->IsUnimportable =
5592 ParentModule && ParentModule->IsUnimportable;
5593 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5594 break;
5595 }
5596
5597 case SUBMODULE_UMBRELLA_HEADER: {
5598 std::string Filename = std::string(Blob);
5599 ResolveImportedPath(F, Filename);
5600 if (auto Umbrella = PP.getFileManager().getOptionalFileRef(Filename)) {
5601 if (!CurrentModule->getUmbrellaHeader())
5602 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5603 else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5604 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5605 Error("mismatched umbrella headers in submodule");
5606 return OutOfDate;
5607 }
5608 }
5609 break;
5610 }
5611
5612 case SUBMODULE_HEADER:
5613 case SUBMODULE_EXCLUDED_HEADER:
5614 case SUBMODULE_PRIVATE_HEADER:
5615 // We lazily associate headers with their modules via the HeaderInfo table.
5616 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5617 // of complete filenames or remove it entirely.
5618 break;
5619
5620 case SUBMODULE_TEXTUAL_HEADER:
5621 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5622 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5623 // them here.
5624 break;
5625
5626 case SUBMODULE_TOPHEADER:
5627 CurrentModule->addTopHeaderFilename(Blob);
5628 break;
5629
5630 case SUBMODULE_UMBRELLA_DIR: {
5631 std::string Dirname = std::string(Blob);
5632 ResolveImportedPath(F, Dirname);
5633 if (auto Umbrella =
5634 PP.getFileManager().getOptionalDirectoryRef(Dirname)) {
5635 if (!CurrentModule->getUmbrellaDir())
5636 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5637 else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5638 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5639 Error("mismatched umbrella directories in submodule");
5640 return OutOfDate;
5641 }
5642 }
5643 break;
5644 }
5645
5646 case SUBMODULE_METADATA: {
5647 F.BaseSubmoduleID = getTotalNumSubmodules();
5648 F.LocalNumSubmodules = Record[0];
5649 unsigned LocalBaseSubmoduleID = Record[1];
5650 if (F.LocalNumSubmodules > 0) {
5651 // Introduce the global -> local mapping for submodules within this
5652 // module.
5653 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5654
5655 // Introduce the local -> global mapping for submodules within this
5656 // module.
5657 F.SubmoduleRemap.insertOrReplace(
5658 std::make_pair(LocalBaseSubmoduleID,
5659 F.BaseSubmoduleID - LocalBaseSubmoduleID));
5660
5661 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5662 }
5663 break;
5664 }
5665
5666 case SUBMODULE_IMPORTS:
5667 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5668 UnresolvedModuleRef Unresolved;
5669 Unresolved.File = &F;
5670 Unresolved.Mod = CurrentModule;
5671 Unresolved.ID = Record[Idx];
5672 Unresolved.Kind = UnresolvedModuleRef::Import;
5673 Unresolved.IsWildcard = false;
5674 UnresolvedModuleRefs.push_back(Unresolved);
5675 }
5676 break;
5677
5678 case SUBMODULE_EXPORTS:
5679 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5680 UnresolvedModuleRef Unresolved;
5681 Unresolved.File = &F;
5682 Unresolved.Mod = CurrentModule;
5683 Unresolved.ID = Record[Idx];
5684 Unresolved.Kind = UnresolvedModuleRef::Export;
5685 Unresolved.IsWildcard = Record[Idx + 1];
5686 UnresolvedModuleRefs.push_back(Unresolved);
5687 }
5688
5689 // Once we've loaded the set of exports, there's no reason to keep
5690 // the parsed, unresolved exports around.
5691 CurrentModule->UnresolvedExports.clear();
5692 break;
5693
5694 case SUBMODULE_REQUIRES:
5695 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5696 PP.getTargetInfo());
5697 break;
5698
5699 case SUBMODULE_LINK_LIBRARY:
5700 ModMap.resolveLinkAsDependencies(CurrentModule);
5701 CurrentModule->LinkLibraries.push_back(
5702 Module::LinkLibrary(std::string(Blob), Record[0]));
5703 break;
5704
5705 case SUBMODULE_CONFIG_MACRO:
5706 CurrentModule->ConfigMacros.push_back(Blob.str());
5707 break;
5708
5709 case SUBMODULE_CONFLICT: {
5710 UnresolvedModuleRef Unresolved;
5711 Unresolved.File = &F;
5712 Unresolved.Mod = CurrentModule;
5713 Unresolved.ID = Record[0];
5714 Unresolved.Kind = UnresolvedModuleRef::Conflict;
5715 Unresolved.IsWildcard = false;
5716 Unresolved.String = Blob;
5717 UnresolvedModuleRefs.push_back(Unresolved);
5718 break;
5719 }
5720
5721 case SUBMODULE_INITIALIZERS: {
5722 if (!ContextObj)
5723 break;
5724 SmallVector<uint32_t, 16> Inits;
5725 for (auto &ID : Record)
5726 Inits.push_back(getGlobalDeclID(F, ID));
5727 ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5728 break;
5729 }
5730
5731 case SUBMODULE_EXPORT_AS:
5732 CurrentModule->ExportAsModule = Blob.str();
5733 ModMap.addLinkAsDependency(CurrentModule);
5734 break;
5735 }
5736 }
5737}
5738
5739/// Parse the record that corresponds to a LangOptions data
5740/// structure.
5741///
5742/// This routine parses the language options from the AST file and then gives
5743/// them to the AST listener if one is set.
5744///
5745/// \returns true if the listener deems the file unacceptable, false otherwise.
5746bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5747 bool Complain,
5748 ASTReaderListener &Listener,
5749 bool AllowCompatibleDifferences) {
5750 LangOptions LangOpts;
5751 unsigned Idx = 0;
5752#define LANGOPT(Name, Bits, Default, Description) \
5753 LangOpts.Name = Record[Idx++];
5754#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5755 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5756#include "clang/Basic/LangOptions.def"
5757#define SANITIZER(NAME, ID) \
5758 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5759#include "clang/Basic/Sanitizers.def"
5760
5761 for (unsigned N = Record[Idx++]; N; --N)
5762 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5763
5764 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5765 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5766 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5767
5768 LangOpts.CurrentModule = ReadString(Record, Idx);
5769
5770 // Comment options.
5771 for (unsigned N = Record[Idx++]; N; --N) {
5772 LangOpts.CommentOpts.BlockCommandNames.push_back(
5773 ReadString(Record, Idx));
5774 }
5775 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5776
5777 // OpenMP offloading options.
5778 for (unsigned N = Record[Idx++]; N; --N) {
5779 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5780 }
5781
5782 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5783
5784 return Listener.ReadLanguageOptions(LangOpts, Complain,
5785 AllowCompatibleDifferences);
5786}
5787
5788bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5789 ASTReaderListener &Listener,
5790 bool AllowCompatibleDifferences) {
5791 unsigned Idx = 0;
5792 TargetOptions TargetOpts;
5793 TargetOpts.Triple = ReadString(Record, Idx);
5794 TargetOpts.CPU = ReadString(Record, Idx);
5795 TargetOpts.TuneCPU = ReadString(Record, Idx);
5796 TargetOpts.ABI = ReadString(Record, Idx);
5797 for (unsigned N = Record[Idx++]; N; --N) {
5798 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5799 }
5800 for (unsigned N = Record[Idx++]; N; --N) {
5801 TargetOpts.Features.push_back(ReadString(Record, Idx));
5802 }
5803
5804 return Listener.ReadTargetOptions(TargetOpts, Complain,
5805 AllowCompatibleDifferences);
5806}
5807
5808bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5809 ASTReaderListener &Listener) {
5810 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5811 unsigned Idx = 0;
5812#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5813#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5814 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5815#include "clang/Basic/DiagnosticOptions.def"
5816
5817 for (unsigned N = Record[Idx++]; N; --N)
5818 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5819 for (unsigned N = Record[Idx++]; N; --N)
5820 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5821
5822 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5823}
5824
5825bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5826 ASTReaderListener &Listener) {
5827 FileSystemOptions FSOpts;
5828 unsigned Idx = 0;
5829 FSOpts.WorkingDir = ReadString(Record, Idx);
5830 return Listener.ReadFileSystemOptions(FSOpts, Complain);
5831}
5832
5833bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5834 bool Complain,
5835 ASTReaderListener &Listener) {
5836 HeaderSearchOptions HSOpts;
5837 unsigned Idx = 0;
5838 HSOpts.Sysroot = ReadString(Record, Idx);
5839
5840 // Include entries.
5841 for (unsigned N = Record[Idx++]; N; --N) {
5842 std::string Path = ReadString(Record, Idx);
5843 frontend::IncludeDirGroup Group
5844 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5845 bool IsFramework = Record[Idx++];
5846 bool IgnoreSysRoot = Record[Idx++];
5847 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5848 IgnoreSysRoot);
5849 }
5850
5851 // System header prefixes.
5852 for (unsigned N = Record[Idx++]; N; --N) {
5853 std::string Prefix = ReadString(Record, Idx);
5854 bool IsSystemHeader = Record[Idx++];
5855 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5856 }
5857
5858 HSOpts.ResourceDir = ReadString(Record, Idx);
5859 HSOpts.ModuleCachePath = ReadString(Record, Idx);
5860 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5861 HSOpts.DisableModuleHash = Record[Idx++];
5862 HSOpts.ImplicitModuleMaps = Record[Idx++];
5863 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5864 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
5865 HSOpts.UseBuiltinIncludes = Record[Idx++];
5866 HSOpts.UseStandardSystemIncludes = Record[Idx++];
5867 HSOpts.UseStandardCXXIncludes = Record[Idx++];
5868 HSOpts.UseLibcxx = Record[Idx++];
5869 std::string SpecificModuleCachePath = ReadString(Record, Idx);
5870
5871 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5872 Complain);
5873}
5874
5875bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5876 bool Complain,
5877 ASTReaderListener &Listener,
5878 std::string &SuggestedPredefines) {
5879 PreprocessorOptions PPOpts;
5880 unsigned Idx = 0;
5881
5882 // Macro definitions/undefs
5883 for (unsigned N = Record[Idx++]; N; --N) {
5884 std::string Macro = ReadString(Record, Idx);
5885 bool IsUndef = Record[Idx++];
5886 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5887 }
5888
5889 // Includes
5890 for (unsigned N = Record[Idx++]; N; --N) {
5891 PPOpts.Includes.push_back(ReadString(Record, Idx));
5892 }
5893
5894 // Macro Includes
5895 for (unsigned N = Record[Idx++]; N; --N) {
5896 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5897 }
5898
5899 PPOpts.UsePredefines = Record[Idx++];
5900 PPOpts.DetailedRecord = Record[Idx++];
5901 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5902 PPOpts.ObjCXXARCStandardLibrary =
5903 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5904 SuggestedPredefines.clear();
5905 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5906 SuggestedPredefines);
5907}
5908
5909std::pair<ModuleFile *, unsigned>
5910ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5911 GlobalPreprocessedEntityMapType::iterator
5912 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5913 assert(I != GlobalPreprocessedEntityMap.end() &&
5914 "Corrupted global preprocessed entity map");
5915 ModuleFile *M = I->second;
5916 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5917 return std::make_pair(M, LocalIndex);
5918}
5919
5920llvm::iterator_range<PreprocessingRecord::iterator>
5921ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5922 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5923 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5924 Mod.NumPreprocessedEntities);
5925
5926 return llvm::make_range(PreprocessingRecord::iterator(),
5927 PreprocessingRecord::iterator());
5928}
5929
5930llvm::iterator_range<ASTReader::ModuleDeclIterator>
5931ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5932 return llvm::make_range(
5933 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5934 ModuleDeclIterator(this, &Mod,
5935 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5936}
5937
5938SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5939 auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5940 assert(I != GlobalSkippedRangeMap.end() &&
5941 "Corrupted global skipped range map");
5942 ModuleFile *M = I->second;
5943 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5944 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5945 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5946 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5947 TranslateSourceLocation(*M, RawRange.getEnd()));
5948 assert(Range.isValid());
5949 return Range;
5950}
5951
5952PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5953 PreprocessedEntityID PPID = Index+1;
5954 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5955 ModuleFile &M = *PPInfo.first;
5956 unsigned LocalIndex = PPInfo.second;
5957 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5958
5959 if (!PP.getPreprocessingRecord()) {
5960 Error("no preprocessing record");
5961 return nullptr;
5962 }
5963
5964 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5965 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5966 M.MacroOffsetsBase + PPOffs.BitOffset)) {
5967 Error(std::move(Err));
5968 return nullptr;
5969 }
5970
5971 Expected<llvm::BitstreamEntry> MaybeEntry =
5972 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5973 if (!MaybeEntry) {
5974 Error(MaybeEntry.takeError());
5975 return nullptr;
5976 }
5977 llvm::BitstreamEntry Entry = MaybeEntry.get();
5978
5979 if (Entry.Kind != llvm::BitstreamEntry::Record)
5980 return nullptr;
5981
5982 // Read the record.
5983 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5984 TranslateSourceLocation(M, PPOffs.getEnd()));
5985 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5986 StringRef Blob;
5987 RecordData Record;
5988 Expected<unsigned> MaybeRecType =
5989 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5990 if (!MaybeRecType) {
5991 Error(MaybeRecType.takeError());
5992 return nullptr;
5993 }
5994 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5995 case PPD_MACRO_EXPANSION: {
5996 bool isBuiltin = Record[0];
5997 IdentifierInfo *Name = nullptr;
5998 MacroDefinitionRecord *Def = nullptr;
5999 if (isBuiltin)
6000 Name = getLocalIdentifier(M, Record[1]);
6001 else {
6002 PreprocessedEntityID GlobalID =
6003 getGlobalPreprocessedEntityID(M, Record[1]);
6004 Def = cast<MacroDefinitionRecord>(
6005 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
6006 }
6007
6008 MacroExpansion *ME;
6009 if (isBuiltin)
6010 ME = new (PPRec) MacroExpansion(Name, Range);
6011 else
6012 ME = new (PPRec) MacroExpansion(Def, Range);
6013
6014 return ME;
6015 }
6016
6017 case PPD_MACRO_DEFINITION: {
6018 // Decode the identifier info and then check again; if the macro is
6019 // still defined and associated with the identifier,
6020 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6021 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6022
6023 if (DeserializationListener)
6024 DeserializationListener->MacroDefinitionRead(PPID, MD);
6025
6026 return MD;
6027 }
6028
6029 case PPD_INCLUSION_DIRECTIVE: {
6030 const char *FullFileNameStart = Blob.data() + Record[0];
6031 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6032 const FileEntry *File = nullptr;
6033 if (!FullFileName.empty())
6034 if (auto FE = PP.getFileManager().getFile(FullFileName))
6035 File = *FE;
6036
6037 // FIXME: Stable encoding
6038 InclusionDirective::InclusionKind Kind
6039 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6040 InclusionDirective *ID
6041 = new (PPRec) InclusionDirective(PPRec, Kind,
6042 StringRef(Blob.data(), Record[0]),
6043 Record[1], Record[3],
6044 File,
6045 Range);
6046 return ID;
6047 }
6048 }
6049
6050 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6051}
6052
6053/// Find the next module that contains entities and return the ID
6054/// of the first entry.
6055///
6056/// \param SLocMapI points at a chunk of a module that contains no
6057/// preprocessed entities or the entities it contains are not the ones we are
6058/// looking for.
6059PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6060 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6061 ++SLocMapI;
6062 for (GlobalSLocOffsetMapType::const_iterator
6063 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6064 ModuleFile &M = *SLocMapI->second;
6065 if (M.NumPreprocessedEntities)
6066 return M.BasePreprocessedEntityID;
6067 }
6068
6069 return getTotalNumPreprocessedEntities();
6070}
6071
6072namespace {
6073
6074struct PPEntityComp {
6075 const ASTReader &Reader;
6076 ModuleFile &M;
6077
6078 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6079
6080 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6081 SourceLocation LHS = getLoc(L);
6082 SourceLocation RHS = getLoc(R);
6083 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6084 }
6085
6086 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6087 SourceLocation LHS = getLoc(L);
6088 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6089 }
6090
6091 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6092 SourceLocation RHS = getLoc(R);
6093 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6094 }
6095
6096 SourceLocation getLoc(const PPEntityOffset &PPE) const {
6097 return Reader.TranslateSourceLocation(M, PPE.getBegin());
6098 }
6099};
6100
6101} // namespace
6102
6103PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6104 bool EndsAfter) const {
6105 if (SourceMgr.isLocalSourceLocation(Loc))
6106 return getTotalNumPreprocessedEntities();
6107
6108 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6109 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6110 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6111 "Corrupted global sloc offset map");
6112
6113 if (SLocMapI->second->NumPreprocessedEntities == 0)
6114 return findNextPreprocessedEntity(SLocMapI);
6115
6116 ModuleFile &M = *SLocMapI->second;
6117
6118 using pp_iterator = const PPEntityOffset *;
6119
6120 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6121 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6122
6123 size_t Count = M.NumPreprocessedEntities;
6124 size_t Half;
6125 pp_iterator First = pp_begin;
6126 pp_iterator PPI;
6127
6128 if (EndsAfter) {
6129 PPI = std::upper_bound(pp_begin, pp_end, Loc,
6130 PPEntityComp(*this, M));
6131 } else {
6132 // Do a binary search manually instead of using std::lower_bound because
6133 // The end locations of entities may be unordered (when a macro expansion
6134 // is inside another macro argument), but for this case it is not important
6135 // whether we get the first macro expansion or its containing macro.
6136 while (Count > 0) {
6137 Half = Count / 2;
6138 PPI = First;
6139 std::advance(PPI, Half);
6140 if (SourceMgr.isBeforeInTranslationUnit(
6141 TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6142 First = PPI;
6143 ++First;
6144 Count = Count - Half - 1;
6145 } else
6146 Count = Half;
6147 }
6148 }
6149
6150 if (PPI == pp_end)
6151 return findNextPreprocessedEntity(SLocMapI);
6152
6153 return M.BasePreprocessedEntityID + (PPI - pp_begin);
6154}
6155
6156/// Returns a pair of [Begin, End) indices of preallocated
6157/// preprocessed entities that \arg Range encompasses.
6158std::pair<unsigned, unsigned>
6159 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6160 if (Range.isInvalid())
6161 return std::make_pair(0,0);
6162 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6163
6164 PreprocessedEntityID BeginID =
6165 findPreprocessedEntity(Range.getBegin(), false);
6166 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6167 return std::make_pair(BeginID, EndID);
6168}
6169
6170/// Optionally returns true or false if the preallocated preprocessed
6171/// entity with index \arg Index came from file \arg FID.
6172Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6173 FileID FID) {
6174 if (FID.isInvalid())
6175 return false;
6176
6177 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6178 ModuleFile &M = *PPInfo.first;
6179 unsigned LocalIndex = PPInfo.second;
6180 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6181
6182 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6183 if (Loc.isInvalid())
6184 return false;
6185
6186 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6187 return true;
6188 else
6189 return false;
6190}
6191
6192namespace {
6193
6194 /// Visitor used to search for information about a header file.
6195 class HeaderFileInfoVisitor {
6196 const FileEntry *FE;
6197 Optional<HeaderFileInfo> HFI;
6198
6199 public:
6200 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6201
6202 bool operator()(ModuleFile &M) {
6203 HeaderFileInfoLookupTable *Table
6204 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6205 if (!Table)
6206 return false;
6207
6208 // Look in the on-disk hash table for an entry for this file name.
6209 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6210 if (Pos == Table->end())
6211 return false;
6212
6213 HFI = *Pos;
6214 return true;
6215 }
6216
6217 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6218 };
6219
6220} // namespace
6221
6222HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6223 HeaderFileInfoVisitor Visitor(FE);
6224 ModuleMgr.visit(Visitor);
6225 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6226 return *HFI;
6227
6228 return HeaderFileInfo();
6229}
6230
6231void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6232 using DiagState = DiagnosticsEngine::DiagState;
6233 SmallVector<DiagState *, 32> DiagStates;
6234
6235 for (ModuleFile &F : ModuleMgr) {
6236 unsigned Idx = 0;
6237 auto &Record = F.PragmaDiagMappings;
6238 if (Record.empty())
6239 continue;
6240
6241 DiagStates.clear();
6242
6243 auto ReadDiagState =
6244 [&](const DiagState &BasedOn, SourceLocation Loc,
6245 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6246 unsigned BackrefID = Record[Idx++];
6247 if (BackrefID != 0)
6248 return DiagStates[BackrefID - 1];
6249
6250 // A new DiagState was created here.
6251 Diag.DiagStates.push_back(BasedOn);
6252 DiagState *NewState = &Diag.DiagStates.back();
6253 DiagStates.push_back(NewState);
6254 unsigned Size = Record[Idx++];
6255 assert(Idx + Size * 2 <= Record.size() &&
6256 "Invalid data, not enough diag/map pairs");
6257 while (Size--) {
6258 unsigned DiagID = Record[Idx++];
6259 DiagnosticMapping NewMapping =
6260 DiagnosticMapping::deserialize(Record[Idx++]);
6261 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6262 continue;
6263
6264 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6265
6266 // If this mapping was specified as a warning but the severity was
6267 // upgraded due to diagnostic settings, simulate the current diagnostic
6268 // settings (and use a warning).
6269 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6270 NewMapping.setSeverity(diag::Severity::Warning);
6271 NewMapping.setUpgradedFromWarning(false);
6272 }
6273
6274 Mapping = NewMapping;
6275 }
6276 return NewState;
6277 };
6278
6279 // Read the first state.
6280 DiagState *FirstState;
6281 if (F.Kind == MK_ImplicitModule) {
6282 // Implicitly-built modules are reused with different diagnostic
6283 // settings. Use the initial diagnostic state from Diag to simulate this
6284 // compilation's diagnostic settings.
6285 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6286 DiagStates.push_back(FirstState);
6287
6288 // Skip the initial diagnostic state from the serialized module.
6289 assert(Record[1] == 0 &&
6290 "Invalid data, unexpected backref in initial state");
6291 Idx = 3 + Record[2] * 2;
6292 assert(Idx < Record.size() &&
6293 "Invalid data, not enough state change pairs in initial state");
6294 } else if (F.isModule()) {
6295 // For an explicit module, preserve the flags from the module build
6296 // command line (-w, -Weverything, -Werror, ...) along with any explicit
6297 // -Wblah flags.
6298 unsigned Flags = Record[Idx++];
6299 DiagState Initial;
6300 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6301 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6302 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6303 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6304 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6305 Initial.ExtBehavior = (diag::Severity)Flags;
6306 FirstState = ReadDiagState(Initial, SourceLocation(), true);
6307
6308 assert(F.OriginalSourceFileID.isValid());
6309
6310 // Set up the root buffer of the module to start with the initial
6311 // diagnostic state of the module itself, to cover files that contain no
6312 // explicit transitions (for which we did not serialize anything).
6313 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6314 .StateTransitions.push_back({FirstState, 0});
6315 } else {
6316 // For prefix ASTs, start with whatever the user configured on the
6317 // command line.
6318 Idx++; // Skip flags.
6319 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6320 SourceLocation(), false);
6321 }
6322
6323 // Read the state transitions.
6324 unsigned NumLocations = Record[Idx++];
6325 while (NumLocations--) {
6326 assert(Idx < Record.size() &&
6327 "Invalid data, missing pragma diagnostic states");
6328 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6329 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6330 assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6331 assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6332 unsigned Transitions = Record[Idx++];
6333
6334 // Note that we don't need to set up Parent/ParentOffset here, because
6335 // we won't be changing the diagnostic state within imported FileIDs
6336 // (other than perhaps appending to the main source file, which has no
6337 // parent).
6338 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6339 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6340 for (unsigned I = 0; I != Transitions; ++I) {
6341 unsigned Offset = Record[Idx++];
6342 auto *State =
6343 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6344 F.StateTransitions.push_back({State, Offset});
6345 }
6346 }
6347
6348 // Read the final state.
6349 assert(Idx < Record.size() &&
6350 "Invalid data, missing final pragma diagnostic state");
6351 SourceLocation CurStateLoc =
6352 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6353 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6354
6355 if (!F.isModule()) {
6356 Diag.DiagStatesByLoc.CurDiagState = CurState;
6357 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6358
6359 // Preserve the property that the imaginary root file describes the
6360 // current state.
6361 FileID NullFile;
6362 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6363 if (T.empty())
6364 T.push_back({CurState, 0});
6365 else
6366 T[0].State = CurState;
6367 }
6368
6369 // Don't try to read these mappings again.
6370 Record.clear();
6371 }
6372}
6373
6374/// Get the correct cursor and offset for loading a type.
6375ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6376 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6377 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6378 ModuleFile *M = I->second;
6379 return RecordLocation(
6380 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6381 M->DeclsBlockStartOffset);
6382}
6383
6384static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6385 switch (code) {
6386#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6387 case TYPE_##CODE_ID: return Type::CLASS_ID;
6388#include "clang/Serialization/TypeBitCodes.def"
6389 default: return llvm::None;
6390 }
6391}
6392
6393/// Read and return the type with the given index..
6394///
6395/// The index is the type ID, shifted and minus the number of predefs. This
6396/// routine actually reads the record corresponding to the type at the given
6397/// location. It is a helper routine for GetType, which deals with reading type
6398/// IDs.
6399QualType ASTReader::readTypeRecord(unsigned Index) {
6400 assert(ContextObj && "reading type with no AST context");
6401 ASTContext &Context = *ContextObj;
6402 RecordLocation Loc = TypeCursorForIndex(Index);
6403 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6404
6405 // Keep track of where we are in the stream, then jump back there
6406 // after reading this type.
6407 SavedStreamPosition SavedPosition(DeclsCursor);
6408
6409 ReadingKindTracker ReadingKind(Read_Type, *this);
6410
6411 // Note that we are loading a type record.
6412 Deserializing AType(this);
6413
6414 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6415 Error(std::move(Err));
6416 return QualType();
6417 }
6418 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6419 if (!RawCode) {
6420 Error(RawCode.takeError());
6421 return QualType();
6422 }
6423
6424 ASTRecordReader Record(*this, *Loc.F);
6425 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6426 if (!Code) {
6427 Error(Code.takeError());
6428 return QualType();
6429 }
6430 if (Code.get() == TYPE_EXT_QUAL) {
6431 QualType baseType = Record.readQualType();
6432 Qualifiers quals = Record.readQualifiers();
6433 return Context.getQualifiedType(baseType, quals);
6434 }
6435
6436 auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6437 if (!maybeClass) {
6438 Error("Unexpected code for type");
6439 return QualType();
6440 }
6441
6442 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6443 return TypeReader.read(*maybeClass);
6444}
6445
6446namespace clang {
6447
6448class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6449 ASTRecordReader &Reader;
6450
6451 SourceLocation readSourceLocation() {
6452 return Reader.readSourceLocation();
6453 }
6454
6455 TypeSourceInfo *GetTypeSourceInfo() {
6456 return Reader.readTypeSourceInfo();
6457 }
6458
6459 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6460 return Reader.readNestedNameSpecifierLoc();
6461 }
6462
6463 Attr *ReadAttr() {
6464 return Reader.readAttr();
6465 }
6466
6467public:
6468 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6469
6470 // We want compile-time assurance that we've enumerated all of
6471 // these, so unfortunately we have to declare them first, then
6472 // define them out-of-line.
6473#define ABSTRACT_TYPELOC(CLASS, PARENT)
6474#define TYPELOC(CLASS, PARENT) \
6475 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6476#include "clang/AST/TypeLocNodes.def"
6477
6478 void VisitFunctionTypeLoc(FunctionTypeLoc);
6479 void VisitArrayTypeLoc(ArrayTypeLoc);
6480};
6481
6482} // namespace clang
6483
6484void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6485 // nothing to do
6486}
6487
6488void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6489 TL.setBuiltinLoc(readSourceLocation());
6490 if (TL.needsExtraLocalData()) {
6491 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6492 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
6493 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
6494 TL.setModeAttr(Reader.readInt());
6495 }
6496}
6497
6498void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6499 TL.setNameLoc(readSourceLocation());
6500}
6501
6502void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6503 TL.setStarLoc(readSourceLocation());
6504}
6505
6506void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6507 // nothing to do
6508}
6509
6510void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6511 // nothing to do
6512}
6513
6514void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6515 TL.setExpansionLoc(readSourceLocation());
6516}
6517
6518void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6519 TL.setCaretLoc(readSourceLocation());
6520}
6521
6522void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6523 TL.setAmpLoc(readSourceLocation());
6524}
6525
6526void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6527 TL.setAmpAmpLoc(readSourceLocation());
6528}
6529
6530void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6531 TL.setStarLoc(readSourceLocation());
6532 TL.setClassTInfo(GetTypeSourceInfo());
6533}
6534
6535void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6536 TL.setLBracketLoc(readSourceLocation());
6537 TL.setRBracketLoc(readSourceLocation());
6538 if (Reader.readBool())
6539 TL.setSizeExpr(Reader.readExpr());
6540 else
6541 TL.setSizeExpr(nullptr);
6542}
6543
6544void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6545 VisitArrayTypeLoc(TL);
6546}
6547
6548void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6549 VisitArrayTypeLoc(TL);
6550}
6551
6552void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6553 VisitArrayTypeLoc(TL);
6554}
6555
6556void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6557 DependentSizedArrayTypeLoc TL) {
6558 VisitArrayTypeLoc(TL);
6559}
6560
6561void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6562 DependentAddressSpaceTypeLoc TL) {
6563
6564 TL.setAttrNameLoc(readSourceLocation());
6565 TL.setAttrOperandParensRange(Reader.readSourceRange());
6566 TL.setAttrExprOperand(Reader.readExpr());
6567}
6568
6569void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6570 DependentSizedExtVectorTypeLoc TL) {
6571 TL.setNameLoc(readSourceLocation());
6572}
6573
6574void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6575 TL.setNameLoc(readSourceLocation());
6576}
6577
6578void TypeLocReader::VisitDependentVectorTypeLoc(
6579 DependentVectorTypeLoc TL) {
6580 TL.setNameLoc(readSourceLocation());
6581}
6582
6583void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6584 TL.setNameLoc(readSourceLocation());
6585}
6586
6587void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6588 TL.setAttrNameLoc(readSourceLocation());
6589 TL.setAttrOperandParensRange(Reader.readSourceRange());
6590 TL.setAttrRowOperand(Reader.readExpr());
6591 TL.setAttrColumnOperand(Reader.readExpr());
6592}
6593
6594void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6595 DependentSizedMatrixTypeLoc TL) {
6596 TL.setAttrNameLoc(readSourceLocation());
6597 TL.setAttrOperandParensRange(Reader.readSourceRange());
6598 TL.setAttrRowOperand(Reader.readExpr());
6599 TL.setAttrColumnOperand(Reader.readExpr());
6600}
6601
6602void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6603 TL.setLocalRangeBegin(readSourceLocation());
6604 TL.setLParenLoc(readSourceLocation());
6605 TL.setRParenLoc(readSourceLocation());
6606 TL.setExceptionSpecRange(Reader.readSourceRange());
6607 TL.setLocalRangeEnd(readSourceLocation());
6608 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6609 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6610 }
6611}
6612
6613void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6614 VisitFunctionTypeLoc(TL);
6615}
6616
6617void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6618 VisitFunctionTypeLoc(TL);
6619}
6620
6621void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6622 TL.setNameLoc(readSourceLocation());
6623}
6624
6625void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6626 TL.setNameLoc(readSourceLocation());
6627}
6628
6629void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6630 TL.setTypeofLoc(readSourceLocation());
6631 TL.setLParenLoc(readSourceLocation());
6632 TL.setRParenLoc(readSourceLocation());
6633}
6634
6635void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6636 TL.setTypeofLoc(readSourceLocation());
6637 TL.setLParenLoc(readSourceLocation());
6638 TL.setRParenLoc(readSourceLocation());
6639 TL.setUnderlyingTInfo(GetTypeSourceInfo());
6640}
6641
6642void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6643 TL.setNameLoc(readSourceLocation());
6644}
6645
6646void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6647 TL.setKWLoc(readSourceLocation());
6648 TL.setLParenLoc(readSourceLocation());
6649 TL.setRParenLoc(readSourceLocation());
6650 TL.setUnderlyingTInfo(GetTypeSourceInfo());
6651}
6652
6653void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6654 TL.setNameLoc(readSourceLocation());
6655 if (Reader.readBool()) {
6656 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6657 TL.setTemplateKWLoc(readSourceLocation());
6658 TL.setConceptNameLoc(readSourceLocation());
6659 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6660 TL.setLAngleLoc(readSourceLocation());
6661 TL.setRAngleLoc(readSourceLocation());
6662 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6663 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6664 TL.getTypePtr()->getArg(i).getKind()));
6665 }
6666}
6667
6668void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6669 DeducedTemplateSpecializationTypeLoc TL) {
6670 TL.setTemplateNameLoc(readSourceLocation());
6671}
6672
6673void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6674 TL.setNameLoc(readSourceLocation());
6675}
6676
6677void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6678 TL.setNameLoc(readSourceLocation());
6679}
6680
6681void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6682 TL.setAttr(ReadAttr());
6683}
6684
6685void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6686 TL.setNameLoc(readSourceLocation());
6687}
6688
6689void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6690 SubstTemplateTypeParmTypeLoc TL) {
6691 TL.setNameLoc(readSourceLocation());
6692}
6693
6694void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6695 SubstTemplateTypeParmPackTypeLoc TL) {
6696 TL.setNameLoc(readSourceLocation());
6697}
6698
6699void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6700 TemplateSpecializationTypeLoc TL) {
6701 TL.setTemplateKeywordLoc(readSourceLocation());
6702 TL.setTemplateNameLoc(readSourceLocation());
6703 TL.setLAngleLoc(readSourceLocation());
6704 TL.setRAngleLoc(readSourceLocation());
6705 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6706 TL.setArgLocInfo(
6707 i,
6708 Reader.readTemplateArgumentLocInfo(
6709 TL.getTypePtr()->getArg(i).getKind()));
6710}
6711
6712void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6713 TL.setLParenLoc(readSourceLocation());
6714 TL.setRParenLoc(readSourceLocation());
6715}
6716
6717void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6718 TL.setElaboratedKeywordLoc(readSourceLocation());
6719 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6720}
6721
6722void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6723 TL.setNameLoc(readSourceLocation());
6724}
6725
6726void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6727 TL.setElaboratedKeywordLoc(readSourceLocation());
6728 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6729 TL.setNameLoc(readSourceLocation());
6730}
6731
6732void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6733 DependentTemplateSpecializationTypeLoc TL) {
6734 TL.setElaboratedKeywordLoc(readSourceLocation());
6735 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6736 TL.setTemplateKeywordLoc(readSourceLocation());
6737 TL.setTemplateNameLoc(readSourceLocation());
6738 TL.setLAngleLoc(readSourceLocation());
6739 TL.setRAngleLoc(readSourceLocation());
6740 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6741 TL.setArgLocInfo(
6742 I,
6743 Reader.readTemplateArgumentLocInfo(
6744 TL.getTypePtr()->getArg(I).getKind()));
6745}
6746
6747void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6748 TL.setEllipsisLoc(readSourceLocation());
6749}
6750
6751void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6752 TL.setNameLoc(readSourceLocation());
6753}
6754
6755void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6756 if (TL.getNumProtocols()) {
6757 TL.setProtocolLAngleLoc(readSourceLocation());
6758 TL.setProtocolRAngleLoc(readSourceLocation());
6759 }
6760 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6761 TL.setProtocolLoc(i, readSourceLocation());
6762}
6763
6764void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6765 TL.setHasBaseTypeAsWritten(Reader.readBool());
6766 TL.setTypeArgsLAngleLoc(readSourceLocation());
6767 TL.setTypeArgsRAngleLoc(readSourceLocation());
6768 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6769 TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6770 TL.setProtocolLAngleLoc(readSourceLocation());
6771 TL.setProtocolRAngleLoc(readSourceLocation());
6772 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6773 TL.setProtocolLoc(i, readSourceLocation());
6774}
6775
6776void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6777 TL.setStarLoc(readSourceLocation());
6778}
6779
6780void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6781 TL.setKWLoc(readSourceLocation());
6782 TL.setLParenLoc(readSourceLocation());
6783 TL.setRParenLoc(readSourceLocation());
6784}
6785
6786void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6787 TL.setKWLoc(readSourceLocation());
6788}
6789
6790void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6791 TL.setNameLoc(readSourceLocation());
6792}
6793void TypeLocReader::VisitDependentExtIntTypeLoc(
6794 clang::DependentExtIntTypeLoc TL) {
6795 TL.setNameLoc(readSourceLocation());
6796}
6797
6798
6799void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6800 TypeLocReader TLR(*this);
6801 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6802 TLR.Visit(TL);
6803}
6804
6805TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6806 QualType InfoTy = readType();
6807 if (InfoTy.isNull())
6808 return nullptr;
6809
6810 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6811 readTypeLoc(TInfo->getTypeLoc());
6812 return TInfo;
6813}
6814
6815QualType ASTReader::GetType(TypeID ID) {
6816 assert(ContextObj && "reading type with no AST context");
6817 ASTContext &Context = *ContextObj;
6818
6819 unsigned FastQuals = ID & Qualifiers::FastMask;
6820 unsigned Index = ID >> Qualifiers::FastWidth;
6821
6822 if (Index < NUM_PREDEF_TYPE_IDS) {
6823 QualType T;
6824 switch ((PredefinedTypeIDs)Index) {
6825 case PREDEF_TYPE_NULL_ID:
6826 return QualType();
6827 case PREDEF_TYPE_VOID_ID:
6828 T = Context.VoidTy;
6829 break;
6830 case PREDEF_TYPE_BOOL_ID:
6831 T = Context.BoolTy;
6832 break;
6833 case PREDEF_TYPE_CHAR_U_ID:
6834 case PREDEF_TYPE_CHAR_S_ID:
6835 // FIXME: Check that the signedness of CharTy is correct!
6836 T = Context.CharTy;
6837 break;
6838 case PREDEF_TYPE_UCHAR_ID:
6839 T = Context.UnsignedCharTy;
6840 break;
6841 case PREDEF_TYPE_USHORT_ID:
6842 T = Context.UnsignedShortTy;
6843 break;
6844 case PREDEF_TYPE_UINT_ID:
6845 T = Context.UnsignedIntTy;
6846 break;
6847 case PREDEF_TYPE_ULONG_ID:
6848 T = Context.UnsignedLongTy;
6849 break;
6850 case PREDEF_TYPE_ULONGLONG_ID:
6851 T = Context.UnsignedLongLongTy;
6852 break;
6853 case PREDEF_TYPE_UINT128_ID:
6854 T = Context.UnsignedInt128Ty;
6855 break;
6856 case PREDEF_TYPE_SCHAR_ID:
6857 T = Context.SignedCharTy;
6858 break;
6859 case PREDEF_TYPE_WCHAR_ID:
6860 T = Context.WCharTy;
6861 break;
6862 case PREDEF_TYPE_SHORT_ID:
6863 T = Context.ShortTy;
6864 break;
6865 case PREDEF_TYPE_INT_ID:
6866 T = Context.IntTy;
6867 break;
6868 case PREDEF_TYPE_LONG_ID:
6869 T = Context.LongTy;
6870 break;
6871 case PREDEF_TYPE_LONGLONG_ID:
6872 T = Context.LongLongTy;
6873 break;
6874 case PREDEF_TYPE_INT128_ID:
6875 T = Context.Int128Ty;
6876 break;
6877 case PREDEF_TYPE_BFLOAT16_ID:
6878 T = Context.BFloat16Ty;
6879 break;
6880 case PREDEF_TYPE_HALF_ID:
6881 T = Context.HalfTy;
6882 break;
6883 case PREDEF_TYPE_FLOAT_ID:
6884 T = Context.FloatTy;
6885 break;
6886 case PREDEF_TYPE_DOUBLE_ID:
6887 T = Context.DoubleTy;
6888 break;
6889 case PREDEF_TYPE_LONGDOUBLE_ID:
6890 T = Context.LongDoubleTy;
6891 break;
6892 case PREDEF_TYPE_SHORT_ACCUM_ID:
6893 T = Context.ShortAccumTy;
6894 break;
6895 case PREDEF_TYPE_ACCUM_ID:
6896 T = Context.AccumTy;
6897 break;
6898 case PREDEF_TYPE_LONG_ACCUM_ID:
6899 T = Context.LongAccumTy;
6900 break;
6901 case PREDEF_TYPE_USHORT_ACCUM_ID:
6902 T = Context.UnsignedShortAccumTy;
6903 break;
6904 case PREDEF_TYPE_UACCUM_ID:
6905 T = Context.UnsignedAccumTy;
6906 break;
6907 case PREDEF_TYPE_ULONG_ACCUM_ID:
6908 T = Context.UnsignedLongAccumTy;
6909 break;
6910 case PREDEF_TYPE_SHORT_FRACT_ID:
6911 T = Context.ShortFractTy;
6912 break;
6913 case PREDEF_TYPE_FRACT_ID:
6914 T = Context.FractTy;
6915 break;
6916 case PREDEF_TYPE_LONG_FRACT_ID:
6917 T = Context.LongFractTy;
6918 break;
6919 case PREDEF_TYPE_USHORT_FRACT_ID:
6920 T = Context.UnsignedShortFractTy;
6921 break;
6922 case PREDEF_TYPE_UFRACT_ID:
6923 T = Context.UnsignedFractTy;
6924 break;
6925 case PREDEF_TYPE_ULONG_FRACT_ID:
6926 T = Context.UnsignedLongFractTy;
6927 break;
6928 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6929 T = Context.SatShortAccumTy;
6930 break;
6931 case PREDEF_TYPE_SAT_ACCUM_ID:
6932 T = Context.SatAccumTy;
6933 break;
6934 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6935 T = Context.SatLongAccumTy;
6936 break;
6937 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6938 T = Context.SatUnsignedShortAccumTy;
6939 break;
6940 case PREDEF_TYPE_SAT_UACCUM_ID:
6941 T = Context.SatUnsignedAccumTy;
6942 break;
6943 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6944 T = Context.SatUnsignedLongAccumTy;
6945 break;
6946 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6947 T = Context.SatShortFractTy;
6948 break;
6949 case PREDEF_TYPE_SAT_FRACT_ID:
6950 T = Context.SatFractTy;
6951 break;
6952 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6953 T = Context.SatLongFractTy;
6954 break;
6955 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6956 T = Context.SatUnsignedShortFractTy;
6957 break;
6958 case PREDEF_TYPE_SAT_UFRACT_ID:
6959 T = Context.SatUnsignedFractTy;
6960 break;
6961 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6962 T = Context.SatUnsignedLongFractTy;
6963 break;
6964 case PREDEF_TYPE_FLOAT16_ID:
6965 T = Context.Float16Ty;
6966 break;
6967 case PREDEF_TYPE_FLOAT128_ID:
6968 T = Context.Float128Ty;
6969 break;
6970 case PREDEF_TYPE_OVERLOAD_ID:
6971 T = Context.OverloadTy;
6972 break;
6973 case PREDEF_TYPE_BOUND_MEMBER:
6974 T = Context.BoundMemberTy;
6975 break;
6976 case PREDEF_TYPE_PSEUDO_OBJECT:
6977 T = Context.PseudoObjectTy;
6978 break;
6979 case PREDEF_TYPE_DEPENDENT_ID:
6980 T = Context.DependentTy;
6981 break;
6982 case PREDEF_TYPE_UNKNOWN_ANY:
6983 T = Context.UnknownAnyTy;
6984 break;
6985 case PREDEF_TYPE_NULLPTR_ID:
6986 T = Context.NullPtrTy;
6987 break;
6988 case PREDEF_TYPE_CHAR8_ID:
6989 T = Context.Char8Ty;
6990 break;
6991 case PREDEF_TYPE_CHAR16_ID:
6992 T = Context.Char16Ty;
6993 break;
6994 case PREDEF_TYPE_CHAR32_ID:
6995 T = Context.Char32Ty;
6996 break;
6997 case PREDEF_TYPE_OBJC_ID:
6998 T = Context.ObjCBuiltinIdTy;
6999 break;
7000 case PREDEF_TYPE_OBJC_CLASS:
7001 T = Context.ObjCBuiltinClassTy;
7002 break;
7003 case PREDEF_TYPE_OBJC_SEL:
7004 T = Context.ObjCBuiltinSelTy;
7005 break;
7006#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7007 case PREDEF_TYPE_##Id##_ID: \
7008 T = Context.SingletonId; \
7009 break;
7010#include "clang/Basic/OpenCLImageTypes.def"
7011#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7012 case PREDEF_TYPE_##Id##_ID: \
7013 T = Context.Id##Ty; \
7014 break;
7015#include "clang/Basic/OpenCLExtensionTypes.def"
7016 case PREDEF_TYPE_SAMPLER_ID:
7017 T = Context.OCLSamplerTy;
7018 break;
7019 case PREDEF_TYPE_EVENT_ID:
7020 T = Context.OCLEventTy;
7021 break;
7022 case PREDEF_TYPE_CLK_EVENT_ID:
7023 T = Context.OCLClkEventTy;
7024 break;
7025 case PREDEF_TYPE_QUEUE_ID:
7026 T = Context.OCLQueueTy;
7027 break;
7028 case PREDEF_TYPE_RESERVE_ID_ID:
7029 T = Context.OCLReserveIDTy;
7030 break;
7031 case PREDEF_TYPE_AUTO_DEDUCT:
7032 T = Context.getAutoDeductType();
7033 break;
7034 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7035 T = Context.getAutoRRefDeductType();
7036 break;
7037 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7038 T = Context.ARCUnbridgedCastTy;
7039 break;
7040 case PREDEF_TYPE_BUILTIN_FN:
7041 T = Context.BuiltinFnTy;
7042 break;
7043 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7044 T = Context.IncompleteMatrixIdxTy;
7045 break;
7046 case PREDEF_TYPE_OMP_ARRAY_SECTION:
7047 T = Context.OMPArraySectionTy;
7048 break;
7049 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7050 T = Context.OMPArraySectionTy;
7051 break;
7052 case PREDEF_TYPE_OMP_ITERATOR:
7053 T = Context.OMPIteratorTy;
7054 break;
7055#define SVE_TYPE(Name, Id, SingletonId) \
7056 case PREDEF_TYPE_##Id##_ID: \
7057 T = Context.SingletonId; \
7058 break;
7059#include "clang/Basic/AArch64SVEACLETypes.def"
7060#define PPC_VECTOR_TYPE(Name, Id, Size) \
7061 case PREDEF_TYPE_##Id##_ID: \
7062 T = Context.Id##Ty; \
7063 break;
7064#include "clang/Basic/PPCTypes.def"
7065 }
7066
7067 assert(!T.isNull() && "Unknown predefined type");
7068 return T.withFastQualifiers(FastQuals);
7069 }
7070
7071 Index -= NUM_PREDEF_TYPE_IDS;
7072 assert(Index < TypesLoaded.size() && "Type index out-of-range");
7073 if (TypesLoaded[Index].isNull()) {
7074 TypesLoaded[Index] = readTypeRecord(Index);
7075 if (TypesLoaded[Index].isNull())
7076 return QualType();
7077
7078 TypesLoaded[Index]->setFromAST();
7079 if (DeserializationListener)
7080 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7081 TypesLoaded[Index]);
7082 }
7083
7084 return TypesLoaded[Index].withFastQualifiers(FastQuals);
7085}
7086
7087QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7088 return GetType(getGlobalTypeID(F, LocalID));
7089}
7090
7091serialization::TypeID
7092ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7093 unsigned FastQuals = LocalID & Qualifiers::FastMask;
7094 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7095
7096 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7097 return LocalID;
7098
7099 if (!F.ModuleOffsetMap.empty())
7100 ReadModuleOffsetMap(F);
7101
7102 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7103 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7104 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7105
7106 unsigned GlobalIndex = LocalIndex + I->second;
7107 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7108}
7109
7110TemplateArgumentLocInfo
7111ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7112 switch (Kind) {
7113 case TemplateArgument::Expression:
7114 return readExpr();
7115 case TemplateArgument::Type:
7116 return readTypeSourceInfo();
7117 case TemplateArgument::Template: {
7118 NestedNameSpecifierLoc QualifierLoc =
7119 readNestedNameSpecifierLoc();
7120 SourceLocation TemplateNameLoc = readSourceLocation();
7121 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7122 TemplateNameLoc, SourceLocation());
7123 }
7124 case TemplateArgument::TemplateExpansion: {
7125 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7126 SourceLocation TemplateNameLoc = readSourceLocation();
7127 SourceLocation EllipsisLoc = readSourceLocation();
7128 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7129 TemplateNameLoc, EllipsisLoc);
7130 }
7131 case TemplateArgument::Null:
7132 case TemplateArgument::Integral:
7133 case TemplateArgument::Declaration:
7134 case TemplateArgument::NullPtr:
7135 case TemplateArgument::Pack:
7136 // FIXME: Is this right?
7137 return TemplateArgumentLocInfo();
7138 }
7139 llvm_unreachable("unexpected template argument loc");
7140}
7141
7142TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7143 TemplateArgument Arg = readTemplateArgument();
7144
7145 if (Arg.getKind() == TemplateArgument::Expression) {
7146 if (readBool()) // bool InfoHasSameExpr.
7147 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7148 }
7149 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7150}
7151
7152const ASTTemplateArgumentListInfo *
7153ASTRecordReader::readASTTemplateArgumentListInfo() {
7154 SourceLocation LAngleLoc = readSourceLocation();
7155 SourceLocation RAngleLoc = readSourceLocation();
7156 unsigned NumArgsAsWritten = readInt();
7157 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7158 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7159 TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7160 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7161}
7162
7163Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7164 return GetDecl(ID);
7165}
7166
7167void ASTReader::CompleteRedeclChain(const Decl *D) {
7168 if (NumCurrentElementsDeserializing) {
7169 // We arrange to not care about the complete redeclaration chain while we're
7170 // deserializing. Just remember that the AST has marked this one as complete
7171 // but that it's not actually complete yet, so we know we still need to
7172 // complete it later.
7173 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7174 return;
7175 }
7176
7177 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7178
7179 // If this is a named declaration, complete it by looking it up
7180 // within its context.
7181 //
7182 // FIXME: Merging a function definition should merge
7183 // all mergeable entities within it.
7184 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7185 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7186 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7187 if (!getContext().getLangOpts().CPlusPlus &&
7188 isa<TranslationUnitDecl>(DC)) {
7189 // Outside of C++, we don't have a lookup table for the TU, so update
7190 // the identifier instead. (For C++ modules, we don't store decls
7191 // in the serialized identifier table, so we do the lookup in the TU.)
7192 auto *II = Name.getAsIdentifierInfo();
7193 assert(II && "non-identifier name in C?");
7194 if (II->isOutOfDate())
7195 updateOutOfDateIdentifier(*II);
7196 } else
7197 DC->lookup(Name);
7198 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7199 // Find all declarations of this kind from the relevant context.
7200 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7201 auto *DC = cast<DeclContext>(DCDecl);
7202 SmallVector<Decl*, 8> Decls;
7203 FindExternalLexicalDecls(
7204 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7205 }
7206 }
7207 }
7208
7209 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7210 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7211 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7212 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7213 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7214 if (auto *Template = FD->getPrimaryTemplate())
7215 Template->LoadLazySpecializations();
7216 }
7217}
7218
7219CXXCtorInitializer **
7220ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7221 RecordLocation Loc = getLocalBitOffset(Offset);
7222 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7223 SavedStreamPosition SavedPosition(Cursor);
7224 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7225 Error(std::move(Err));
7226 return nullptr;
7227 }
7228 ReadingKindTracker ReadingKind(Read_Decl, *this);
7229
7230 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7231 if (!MaybeCode) {
7232 Error(MaybeCode.takeError());
7233 return nullptr;
7234 }
7235 unsigned Code = MaybeCode.get();
7236
7237 ASTRecordReader Record(*this, *Loc.F);
7238 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7239 if (!MaybeRecCode) {
7240 Error(MaybeRecCode.takeError());
7241 return nullptr;
7242 }
7243 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7244 Error("malformed AST file: missing C++ ctor initializers");
7245 return nullptr;
7246 }
7247
7248 return Record.readCXXCtorInitializers();
7249}
7250
7251CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7252 assert(ContextObj && "reading base specifiers with no AST context");
7253 ASTContext &Context = *ContextObj;
7254
7255 RecordLocation Loc = getLocalBitOffset(Offset);
7256 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7257 SavedStreamPosition SavedPosition(Cursor);
7258 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7259 Error(std::move(Err));
7260 return nullptr;
7261 }
7262 ReadingKindTracker ReadingKind(Read_Decl, *this);
7263
7264 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7265 if (!MaybeCode) {
7266 Error(MaybeCode.takeError());
7267 return nullptr;
7268 }
7269 unsigned Code = MaybeCode.get();
7270
7271 ASTRecordReader Record(*this, *Loc.F);
7272 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7273 if (!MaybeRecCode) {
7274 Error(MaybeCode.takeError());
7275 return nullptr;
7276 }
7277 unsigned RecCode = MaybeRecCode.get();
7278
7279 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7280 Error("malformed AST file: missing C++ base specifiers");
7281 return nullptr;
7282 }
7283
7284 unsigned NumBases = Record.readInt();
7285 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7286 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7287 for (unsigned I = 0; I != NumBases; ++I)
7288 Bases[I] = Record.readCXXBaseSpecifier();
7289 return Bases;
7290}
7291
7292serialization::DeclID
7293ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7294 if (LocalID < NUM_PREDEF_DECL_IDS)
7295 return LocalID;
7296
7297 if (!F.ModuleOffsetMap.empty())
7298 ReadModuleOffsetMap(F);
7299
7300 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7301 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7302 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7303
7304 return LocalID + I->second;
7305}
7306
7307bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7308 ModuleFile &M) const {
7309 // Predefined decls aren't from any module.
7310 if (ID < NUM_PREDEF_DECL_IDS)
7311 return false;
7312
7313 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7314 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7315}
7316
7317ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7318 if (!D->isFromASTFile())
7319 return nullptr;
7320 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7321 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7322 return I->second;
7323}
7324
7325SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7326 if (ID < NUM_PREDEF_DECL_IDS)
7327 return SourceLocation();
7328
7329 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7330
7331 if (Index > DeclsLoaded.size()) {
7332 Error("declaration ID out-of-range for AST file");
7333 return SourceLocation();
7334 }
7335
7336 if (Decl *D = DeclsLoaded[Index])
7337 return D->getLocation();
7338
7339 SourceLocation Loc;
7340 DeclCursorForID(ID, Loc);
7341 return Loc;
7342}
7343
7344static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7345 switch (ID) {
7346 case PREDEF_DECL_NULL_ID:
7347 return nullptr;
7348
7349 case PREDEF_DECL_TRANSLATION_UNIT_ID:
7350 return Context.getTranslationUnitDecl();
7351
7352 case PREDEF_DECL_OBJC_ID_ID:
7353 return Context.getObjCIdDecl();
7354
7355 case PREDEF_DECL_OBJC_SEL_ID:
7356 return Context.getObjCSelDecl();
7357
7358 case PREDEF_DECL_OBJC_CLASS_ID:
7359 return Context.getObjCClassDecl();
7360
7361 case PREDEF_DECL_OBJC_PROTOCOL_ID:
7362 return Context.getObjCProtocolDecl();
7363
7364 case PREDEF_DECL_INT_128_ID:
7365 return Context.getInt128Decl();
7366
7367 case PREDEF_DECL_UNSIGNED_INT_128_ID:
7368 return Context.getUInt128Decl();
7369
7370 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7371 return Context.getObjCInstanceTypeDecl();
7372
7373 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7374 return Context.getBuiltinVaListDecl();
7375
7376 case PREDEF_DECL_VA_LIST_TAG:
7377 return Context.getVaListTagDecl();
7378
7379 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7380 return Context.getBuiltinMSVaListDecl();
7381
7382 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7383 return Context.getMSGuidTagDecl();
7384
7385 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7386 return Context.getExternCContextDecl();
7387
7388 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7389 return Context.getMakeIntegerSeqDecl();
7390
7391 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7392 return Context.getCFConstantStringDecl();
7393
7394 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7395 return Context.getCFConstantStringTagDecl();
7396
7397 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7398 return Context.getTypePackElementDecl();
7399 }
7400 llvm_unreachable("PredefinedDeclIDs unknown enum value");
7401}
7402
7403Decl *ASTReader::GetExistingDecl(DeclID ID) {
7404 assert(ContextObj && "reading decl with no AST context");
7405 if (ID < NUM_PREDEF_DECL_IDS) {
7406 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7407 if (D) {
7408 // Track that we have merged the declaration with ID \p ID into the
7409 // pre-existing predefined declaration \p D.
7410 auto &Merged = KeyDecls[D->getCanonicalDecl()];
7411 if (Merged.empty())
7412 Merged.push_back(ID);
7413 }
7414 return D;
7415 }
7416
7417 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7418
7419 if (Index >= DeclsLoaded.size()) {
7420 assert(0 && "declaration ID out-of-range for AST file");
7421 Error("declaration ID out-of-range for AST file");
7422 return nullptr;
7423 }
7424
7425 return DeclsLoaded[Index];
7426}
7427
7428Decl *ASTReader::GetDecl(DeclID ID) {
7429 if (ID < NUM_PREDEF_DECL_IDS)
7430 return GetExistingDecl(ID);
7431
7432 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7433
7434 if (Index >= DeclsLoaded.size()) {
7435 assert(0 && "declaration ID out-of-range for AST file");
7436 Error("declaration ID out-of-range for AST file");
7437 return nullptr;
7438 }
7439
7440 if (!DeclsLoaded[Index]) {
7441 ReadDeclRecord(ID);
7442 if (DeserializationListener)
7443 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7444 }
7445
7446 return DeclsLoaded[Index];
7447}
7448
7449DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7450 DeclID GlobalID) {
7451 if (GlobalID < NUM_PREDEF_DECL_IDS)
7452 return GlobalID;
7453
7454 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7455 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7456 ModuleFile *Owner = I->second;
7457
7458 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7459 = M.GlobalToLocalDeclIDs.find(Owner);
7460 if (Pos == M.GlobalToLocalDeclIDs.end())
7461 return 0;
7462
7463 return GlobalID - Owner->BaseDeclID + Pos->second;
7464}
7465
7466serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7467 const RecordData &Record,
7468 unsigned &Idx) {
7469 if (Idx >= Record.size()) {
7470 Error("Corrupted AST file");
7471 return 0;
7472 }
7473
7474 return getGlobalDeclID(F, Record[Idx++]);
7475}
7476
7477/// Resolve the offset of a statement into a statement.
7478///
7479/// This operation will read a new statement from the external
7480/// source each time it is called, and is meant to be used via a
7481/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7482Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7483 // Switch case IDs are per Decl.
7484 ClearSwitchCaseIDs();
7485
7486 // Offset here is a global offset across the entire chain.
7487 RecordLocation Loc = getLocalBitOffset(Offset);
7488 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7489 Error(std::move(Err));
7490 return nullptr;
7491 }
7492 assert(NumCurrentElementsDeserializing == 0 &&
7493 "should not be called while already deserializing");
7494 Deserializing D(this);
7495 return ReadStmtFromStream(*Loc.F);
7496}
7497
7498void ASTReader::FindExternalLexicalDecls(
7499 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7500 SmallVectorImpl<Decl *> &Decls) {
7501 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7502
7503 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7504 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7505 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7506 auto K = (Decl::Kind)+LexicalDecls[I];
7507 if (!IsKindWeWant(K))
7508 continue;
7509
7510 auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7511
7512 // Don't add predefined declarations to the lexical context more
7513 // than once.
7514 if (ID < NUM_PREDEF_DECL_IDS) {
7515 if (PredefsVisited[ID])
7516 continue;
7517
7518 PredefsVisited[ID] = true;
7519 }
7520
7521 if (Decl *D = GetLocalDecl(*M, ID)) {
7522 assert(D->getKind() == K && "wrong kind for lexical decl");
7523 if (!DC->isDeclInLexicalTraversal(D))
7524 Decls.push_back(D);
7525 }
7526 }
7527 };
7528
7529 if (isa<TranslationUnitDecl>(DC)) {
7530 for (auto Lexical : TULexicalDecls)
7531 Visit(Lexical.first, Lexical.second);
7532 } else {
7533 auto I = LexicalDecls.find(DC);
7534 if (I != LexicalDecls.end())
7535 Visit(I->second.first, I->second.second);
7536 }
7537
7538 ++NumLexicalDeclContextsRead;
7539}
7540
7541namespace {
7542
7543class DeclIDComp {
7544 ASTReader &Reader;
7545 ModuleFile &Mod;
7546
7547public:
7548 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7549
7550 bool operator()(LocalDeclID L, LocalDeclID R) const {
7551 SourceLocation LHS = getLocation(L);
7552 SourceLocation RHS = getLocation(R);
7553 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7554 }
7555
7556 bool operator()(SourceLocation LHS, LocalDeclID R) const {
7557 SourceLocation RHS = getLocation(R);
7558 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7559 }
7560
7561 bool operator()(LocalDeclID L, SourceLocation RHS) const {
7562 SourceLocation LHS = getLocation(L);
7563 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7564 }
7565
7566 SourceLocation getLocation(LocalDeclID ID) const {
7567 return Reader.getSourceManager().getFileLoc(
7568 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7569 }
7570};
7571
7572} // namespace
7573
7574void ASTReader::FindFileRegionDecls(FileID File,
7575 unsigned Offset, unsigned Length,
7576 SmallVectorImpl<Decl *> &Decls) {
7577 SourceManager &SM = getSourceManager();
7578
7579 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7580 if (I == FileDeclIDs.end())
7581 return;
7582
7583 FileDeclsInfo &DInfo = I->second;
7584 if (DInfo.Decls.empty())
7585 return;
7586
7587 SourceLocation
7588 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7589 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7590
7591 DeclIDComp DIDComp(*this, *DInfo.Mod);
7592 ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7593 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7594 if (BeginIt != DInfo.Decls.begin())
7595 --BeginIt;
7596
7597 // If we are pointing at a top-level decl inside an objc container, we need
7598 // to backtrack until we find it otherwise we will fail to report that the
7599 // region overlaps with an objc container.
7600 while (BeginIt != DInfo.Decls.begin() &&
7601 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7602 ->isTopLevelDeclInObjCContainer())
7603 --BeginIt;
7604
7605 ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7606 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7607 if (EndIt != DInfo.Decls.end())
7608 ++EndIt;
7609
7610 for (ArrayRef<serialization::LocalDeclID>::iterator
7611 DIt = BeginIt; DIt != EndIt; ++DIt)
7612 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7613}
7614
7615bool
7616ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7617 DeclarationName Name) {
7618 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7619 "DeclContext has no visible decls in storage");
7620 if (!Name)
7621 return false;
7622
7623 auto It = Lookups.find(DC);
7624 if (It == Lookups.end())
7625 return false;
7626
7627 Deserializing LookupResults(this);
7628
7629 // Load the list of declarations.
7630 SmallVector<NamedDecl *, 64> Decls;
7631 for (DeclID ID : It->second.Table.find(Name)) {
7632 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7633 if (ND->getDeclName() == Name)
7634 Decls.push_back(ND);
7635 }
7636
7637 ++NumVisibleDeclContextsRead;
7638 SetExternalVisibleDeclsForName(DC, Name, Decls);
7639 return !Decls.empty();
7640}
7641
7642void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7643 if (!DC->hasExternalVisibleStorage())
7644 return;
7645
7646 auto It = Lookups.find(DC);
7647 assert(It != Lookups.end() &&
7648 "have external visible storage but no lookup tables");
7649
7650 DeclsMap Decls;
7651
7652 for (DeclID ID : It->second.Table.findAll()) {
7653 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7654 Decls[ND->getDeclName()].push_back(ND);
7655 }
7656
7657 ++NumVisibleDeclContextsRead;
7658
7659 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7660 SetExternalVisibleDeclsForName(DC, I->first, I->second);
7661 }
7662 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7663}
7664
7665const serialization::reader::DeclContextLookupTable *
7666ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7667 auto I = Lookups.find(Primary);
7668 return I == Lookups.end() ? nullptr : &I->second;
7669}
7670
7671/// Under non-PCH compilation the consumer receives the objc methods
7672/// before receiving the implementation, and codegen depends on this.
7673/// We simulate this by deserializing and passing to consumer the methods of the
7674/// implementation before passing the deserialized implementation decl.
7675static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7676 ASTConsumer *Consumer) {
7677 assert(ImplD && Consumer);
7678
7679 for (auto *I : ImplD->methods())
7680 Consumer->HandleInterestingDecl(DeclGroupRef(I));
7681
7682 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7683}
7684
7685void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7686 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7687 PassObjCImplDeclToConsumer(ImplD, Consumer);
7688 else
7689 Consumer->HandleInterestingDecl(DeclGroupRef(D));
7690}
7691
7692void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7693 this->Consumer = Consumer;
7694
7695 if (Consumer)
7696 PassInterestingDeclsToConsumer();
7697
7698 if (DeserializationListener)
7699 DeserializationListener->ReaderInitialized(this);
7700}
7701
7702void ASTReader::PrintStats() {
7703 std::fprintf(stderr, "*** AST File Statistics:\n");
7704
7705 unsigned NumTypesLoaded
7706 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7707 QualType());
7708 unsigned NumDeclsLoaded
7709 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7710 (Decl *)nullptr);
7711 unsigned NumIdentifiersLoaded
7712 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7713 IdentifiersLoaded.end(),
7714 (IdentifierInfo *)nullptr);
7715 unsigned NumMacrosLoaded
7716 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7717 MacrosLoaded.end(),
7718 (MacroInfo *)nullptr);
7719 unsigned NumSelectorsLoaded
7720 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7721 SelectorsLoaded.end(),
7722 Selector());
7723
7724 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7725 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
7726 NumSLocEntriesRead, TotalNumSLocEntries,
7727 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7728 if (!TypesLoaded.empty())
7729 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
7730 NumTypesLoaded, (unsigned)TypesLoaded.size(),
7731 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7732 if (!DeclsLoaded.empty())
7733 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
7734 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7735 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7736 if (!IdentifiersLoaded.empty())
7737 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
7738 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7739 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7740 if (!MacrosLoaded.empty())
7741 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
7742 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7743 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7744 if (!SelectorsLoaded.empty())
7745 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
7746 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7747 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7748 if (TotalNumStatements)
7749 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
7750 NumStatementsRead, TotalNumStatements,
7751 ((float)NumStatementsRead/TotalNumStatements * 100));
7752 if (TotalNumMacros)
7753 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
7754 NumMacrosRead, TotalNumMacros,
7755 ((float)NumMacrosRead/TotalNumMacros * 100));
7756 if (TotalLexicalDeclContexts)
7757 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
7758 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7759 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7760 * 100));
7761 if (TotalVisibleDeclContexts)
7762 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
7763 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7764 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7765 * 100));
7766 if (TotalNumMethodPoolEntries)
7767 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
7768 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7769 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7770 * 100));
7771 if (NumMethodPoolLookups)
7772 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
7773 NumMethodPoolHits, NumMethodPoolLookups,
7774 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7775 if (NumMethodPoolTableLookups)
7776 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
7777 NumMethodPoolTableHits, NumMethodPoolTableLookups,
7778 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7779 * 100.0));
7780 if (NumIdentifierLookupHits)
7781 std::fprintf(stderr,
7782 " %u / %u identifier table lookups succeeded (%f%%)\n",
7783 NumIdentifierLookupHits, NumIdentifierLookups,
7784 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7785
7786 if (GlobalIndex) {
7787 std::fprintf(stderr, "\n");
7788 GlobalIndex->printStats();
7789 }
7790
7791 std::fprintf(stderr, "\n");
7792 dump();
7793 std::fprintf(stderr, "\n");
7794}
7795
7796template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7797LLVM_DUMP_METHOD static void
7798dumpModuleIDMap(StringRef Name,
7799 const ContinuousRangeMap<Key, ModuleFile *,
7800 InitialCapacity> &Map) {
7801 if (Map.begin() == Map.end())
7802 return;
7803
7804 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7805
7806 llvm::errs() << Name << ":\n";
7807 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7808 I != IEnd; ++I) {
7809 llvm::errs() << " " << I->first << " -> " << I->second->FileName
7810 << "\n";
7811 }
7812}
7813
7814LLVM_DUMP_METHOD void ASTReader::dump() {
7815 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7816 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7817 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7818 dumpModuleIDMap("Global type map", GlobalTypeMap);
7819 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7820 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7821 dumpModuleIDMap("Global macro map", GlobalMacroMap);
7822 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7823 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7824 dumpModuleIDMap("Global preprocessed entity map",
7825 GlobalPreprocessedEntityMap);
7826
7827 llvm::errs() << "\n*** PCH/Modules Loaded:";
7828 for (ModuleFile &M : ModuleMgr)
7829 M.dump();
7830}
7831
7832/// Return the amount of memory used by memory buffers, breaking down
7833/// by heap-backed versus mmap'ed memory.
7834void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7835 for (ModuleFile &I : ModuleMgr) {
7836 if (llvm::MemoryBuffer *buf = I.Buffer) {
7837 size_t bytes = buf->getBufferSize();
7838 switch (buf->getBufferKind()) {
7839 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7840 sizes.malloc_bytes += bytes;
7841 break;
7842 case llvm::MemoryBuffer::MemoryBuffer_MMap:
7843 sizes.mmap_bytes += bytes;
7844 break;
7845 }
7846 }
7847 }
7848}
7849
7850void ASTReader::InitializeSema(Sema &S) {
7851 SemaObj = &S;
7852 S.addExternalSource(this);
7853
7854 // Makes sure any declarations that were deserialized "too early"
7855 // still get added to the identifier's declaration chains.
7856 for (uint64_t ID : PreloadedDeclIDs) {
7857 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7858 pushExternalDeclIntoScope(D, D->getDeclName());
7859 }
7860 PreloadedDeclIDs.clear();
7861
7862 // FIXME: What happens if these are changed by a module import?
7863 if (!FPPragmaOptions.empty()) {
7864 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7865 FPOptionsOverride NewOverrides =
7866 FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
7867 SemaObj->CurFPFeatures =
7868 NewOverrides.applyOverrides(SemaObj->getLangOpts());
7869 }
7870
7871 SemaObj->OpenCLFeatures = OpenCLExtensions;
7872 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7873 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
7874
7875 UpdateSema();
7876}
7877
7878void ASTReader::UpdateSema() {
7879 assert(SemaObj && "no Sema to update");
7880
7881 // Load the offsets of the declarations that Sema references.
7882 // They will be lazily deserialized when needed.
7883 if (!SemaDeclRefs.empty()) {
7884 assert(SemaDeclRefs.size() % 3 == 0);
7885 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7886 if (!SemaObj->StdNamespace)
7887 SemaObj->StdNamespace = SemaDeclRefs[I];
7888 if (!SemaObj->StdBadAlloc)
7889 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7890 if (!SemaObj->StdAlignValT)
7891 SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7892 }
7893 SemaDeclRefs.clear();
7894 }
7895
7896 // Update the state of pragmas. Use the same API as if we had encountered the
7897 // pragma in the source.
7898 if(OptimizeOffPragmaLocation.isValid())
7899 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7900 if (PragmaMSStructState != -1)
7901 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7902 if (PointersToMembersPragmaLocation.isValid()) {
7903 SemaObj->ActOnPragmaMSPointersToMembers(
7904 (LangOptions::PragmaMSPointersToMembersKind)
7905 PragmaMSPointersToMembersState,
7906 PointersToMembersPragmaLocation);
7907 }
7908 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7909
7910 if (PragmaAlignPackCurrentValue) {
7911 // The bottom of the stack might have a default value. It must be adjusted
7912 // to the current value to ensure that the packing state is preserved after
7913 // popping entries that were included/imported from a PCH/module.
7914 bool DropFirst = false;
7915 if (!PragmaAlignPackStack.empty() &&
7916 PragmaAlignPackStack.front().Location.isInvalid()) {
7917 assert(PragmaAlignPackStack.front().Value ==
7918 SemaObj->AlignPackStack.DefaultValue &&
7919 "Expected a default alignment value");
7920 SemaObj->AlignPackStack.Stack.emplace_back(
7921 PragmaAlignPackStack.front().SlotLabel,
7922 SemaObj->AlignPackStack.CurrentValue,
7923 SemaObj->AlignPackStack.CurrentPragmaLocation,
7924 PragmaAlignPackStack.front().PushLocation);
7925 DropFirst = true;
7926 }
7927 for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack)
7928 .drop_front(DropFirst ? 1 : 0)) {
7929 SemaObj->AlignPackStack.Stack.emplace_back(
7930 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7931 }
7932 if (PragmaAlignPackCurrentLocation.isInvalid()) {
7933 assert(*PragmaAlignPackCurrentValue ==
7934 SemaObj->AlignPackStack.DefaultValue &&
7935 "Expected a default align and pack value");
7936 // Keep the current values.
7937 } else {
7938 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
7939 SemaObj->AlignPackStack.CurrentPragmaLocation =
7940 PragmaAlignPackCurrentLocation;
7941 }
7942 }
7943 if (FpPragmaCurrentValue) {
7944 // The bottom of the stack might have a default value. It must be adjusted
7945 // to the current value to ensure that fp-pragma state is preserved after
7946 // popping entries that were included/imported from a PCH/module.
7947 bool DropFirst = false;
7948 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7949 assert(FpPragmaStack.front().Value ==
7950 SemaObj->FpPragmaStack.DefaultValue &&
7951 "Expected a default pragma float_control value");
7952 SemaObj->FpPragmaStack.Stack.emplace_back(
7953 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7954 SemaObj->FpPragmaStack.CurrentPragmaLocation,
7955 FpPragmaStack.front().PushLocation);
7956 DropFirst = true;
7957 }
7958 for (const auto &Entry :
7959 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7960 SemaObj->FpPragmaStack.Stack.emplace_back(
7961 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7962 if (FpPragmaCurrentLocation.isInvalid()) {
7963 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7964 "Expected a default pragma float_control value");
7965 // Keep the current values.
7966 } else {
7967 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7968 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7969 }
7970 }
7971
7972 // For non-modular AST files, restore visiblity of modules.
7973 for (auto &Import : ImportedModules) {
7974 if (Import.ImportLoc.isInvalid())
7975 continue;
7976 if (Module *Imported = getSubmodule(Import.ID)) {
7977 SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
7978 }
7979 }
7980}
7981
7982IdentifierInfo *ASTReader::get(StringRef Name) {
7983 // Note that we are loading an identifier.
7984 Deserializing AnIdentifier(this);
7985
7986 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7987 NumIdentifierLookups,
7988 NumIdentifierLookupHits);
7989
7990 // We don't need to do identifier table lookups in C++ modules (we preload
7991 // all interesting declarations, and don't need to use the scope for name
7992 // lookups). Perform the lookup in PCH files, though, since we don't build
7993 // a complete initial identifier table if we're carrying on from a PCH.
7994 if (PP.getLangOpts().CPlusPlus) {
7995 for (auto F : ModuleMgr.pch_modules())
7996 if (Visitor(*F))
7997 break;
7998 } else {
7999 // If there is a global index, look there first to determine which modules
8000 // provably do not have any results for this identifier.
8001 GlobalModuleIndex::HitSet Hits;
8002 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8003 if (!loadGlobalIndex()) {
8004 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8005 HitsPtr = &Hits;
8006 }
8007 }
8008
8009 ModuleMgr.visit(Visitor, HitsPtr);
8010 }
8011
8012 IdentifierInfo *II = Visitor.getIdentifierInfo();
8013 markIdentifierUpToDate(II);
8014 return II;
8015}
8016
8017namespace clang {
8018
8019 /// An identifier-lookup iterator that enumerates all of the
8020 /// identifiers stored within a set of AST files.
8021 class ASTIdentifierIterator : public IdentifierIterator {
8022 /// The AST reader whose identifiers are being enumerated.
8023 const ASTReader &Reader;
8024
8025 /// The current index into the chain of AST files stored in
8026 /// the AST reader.
8027 unsigned Index;
8028
8029 /// The current position within the identifier lookup table
8030 /// of the current AST file.
8031 ASTIdentifierLookupTable::key_iterator Current;
8032
8033 /// The end position within the identifier lookup table of
8034 /// the current AST file.
8035 ASTIdentifierLookupTable::key_iterator End;
8036
8037 /// Whether to skip any modules in the ASTReader.
8038 bool SkipModules;
8039
8040 public:
8041 explicit ASTIdentifierIterator(const ASTReader &Reader,
8042 bool SkipModules = false);
8043
8044 StringRef Next() override;
8045 };
8046
8047} // namespace clang
8048
8049ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8050 bool SkipModules)
8051 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8052}
8053
8054StringRef ASTIdentifierIterator::Next() {
8055 while (Current == End) {
8056 // If we have exhausted all of our AST files, we're done.
8057 if (Index == 0)
8058 return StringRef();
8059
8060 --Index;
8061 ModuleFile &F = Reader.ModuleMgr[Index];
8062 if (SkipModules && F.isModule())
8063 continue;
8064
8065 ASTIdentifierLookupTable *IdTable =
8066 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8067 Current = IdTable->key_begin();
8068 End = IdTable->key_end();
8069 }
8070
8071 // We have any identifiers remaining in the current AST file; return
8072 // the next one.
8073 StringRef Result = *Current;
8074 ++Current;
8075 return Result;
8076}
8077
8078namespace {
8079
8080/// A utility for appending two IdentifierIterators.
8081class ChainedIdentifierIterator : public IdentifierIterator {
8082 std::unique_ptr<IdentifierIterator> Current;
8083 std::unique_ptr<IdentifierIterator> Queued;
8084
8085public:
8086 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8087 std::unique_ptr<IdentifierIterator> Second)
8088 : Current(std::move(First)), Queued(std::move(Second)) {}
8089
8090 StringRef Next() override {
8091 if (!Current)
8092 return StringRef();
8093
8094 StringRef result = Current->Next();
8095 if (!result.empty())
8096 return result;
8097
8098 // Try the queued iterator, which may itself be empty.
8099 Current.reset();
8100 std::swap(Current, Queued);
8101 return Next();
8102 }
8103};
8104
8105} // namespace
8106
8107IdentifierIterator *ASTReader::getIdentifiers() {
8108 if (!loadGlobalIndex()) {
8109 std::unique_ptr<IdentifierIterator> ReaderIter(
8110 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8111 std::unique_ptr<IdentifierIterator> ModulesIter(
8112 GlobalIndex->createIdentifierIterator());
8113 return new ChainedIdentifierIterator(std::move(ReaderIter),
8114 std::move(ModulesIter));
8115 }
8116
8117 return new ASTIdentifierIterator(*this);
8118}
8119
8120namespace clang {
8121namespace serialization {
8122
8123 class ReadMethodPoolVisitor {
8124 ASTReader &Reader;
8125 Selector Sel;
8126 unsigned PriorGeneration;
8127 unsigned InstanceBits = 0;
8128 unsigned FactoryBits = 0;
8129 bool InstanceHasMoreThanOneDecl = false;
8130 bool FactoryHasMoreThanOneDecl = false;
8131 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8132 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8133
8134 public:
8135 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8136 unsigned PriorGeneration)
8137 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8138
8139 bool operator()(ModuleFile &M) {
8140 if (!M.SelectorLookupTable)
8141 return false;
8142
8143 // If we've already searched this module file, skip it now.
8144 if (M.Generation <= PriorGeneration)
8145 return true;
8146
8147 ++Reader.NumMethodPoolTableLookups;
8148 ASTSelectorLookupTable *PoolTable
8149 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8150 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8151 if (Pos == PoolTable->end())
8152 return false;
8153
8154 ++Reader.NumMethodPoolTableHits;
8155 ++Reader.NumSelectorsRead;
8156 // FIXME: Not quite happy with the statistics here. We probably should
8157 // disable this tracking when called via LoadSelector.
8158 // Also, should entries without methods count as misses?
8159 ++Reader.NumMethodPoolEntriesRead;
8160 ASTSelectorLookupTrait::data_type Data = *Pos;
8161 if (Reader.DeserializationListener)
8162 Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8163
8164 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8165 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8166 InstanceBits = Data.InstanceBits;
8167 FactoryBits = Data.FactoryBits;
8168 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8169 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8170 return true;
8171 }
8172
8173 /// Retrieve the instance methods found by this visitor.
8174 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8175 return InstanceMethods;
8176 }
8177
8178 /// Retrieve the instance methods found by this visitor.
8179 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8180 return FactoryMethods;
8181 }
8182
8183 unsigned getInstanceBits() const { return InstanceBits; }
8184 unsigned getFactoryBits() const { return FactoryBits; }
8185
8186 bool instanceHasMoreThanOneDecl() const {
8187 return InstanceHasMoreThanOneDecl;
8188 }
8189
8190 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8191 };
8192
8193} // namespace serialization
8194} // namespace clang
8195
8196/// Add the given set of methods to the method list.
8197static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8198 ObjCMethodList &List) {
8199 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8200 S.addMethodToGlobalList(&List, Methods[I]);
8201 }
8202}
8203
8204void ASTReader::ReadMethodPool(Selector Sel) {
8205 // Get the selector generation and update it to the current generation.
8206 unsigned &Generation = SelectorGeneration[Sel];
8207 unsigned PriorGeneration = Generation;
8208 Generation = getGeneration();
8209 SelectorOutOfDate[Sel] = false;
8210
8211 // Search for methods defined with this selector.
8212 ++NumMethodPoolLookups;
8213 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8214 ModuleMgr.visit(Visitor);
8215
8216 if (Visitor.getInstanceMethods().empty() &&
8217 Visitor.getFactoryMethods().empty())
8218 return;
8219
8220 ++NumMethodPoolHits;
8221
8222 if (!getSema())
8223 return;
8224
8225 Sema &S = *getSema();
8226 Sema::GlobalMethodPool::iterator Pos
8227 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8228
8229 Pos->second.first.setBits(Visitor.getInstanceBits());
8230 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8231 Pos->second.second.setBits(Visitor.getFactoryBits());
8232 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8233
8234 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8235 // when building a module we keep every method individually and may need to
8236 // update hasMoreThanOneDecl as we add the methods.
8237 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8238 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8239}
8240
8241void ASTReader::updateOutOfDateSelector(Selector Sel) {
8242 if (SelectorOutOfDate[Sel])
8243 ReadMethodPool(Sel);
8244}
8245
8246void ASTReader::ReadKnownNamespaces(
8247 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8248 Namespaces.clear();
8249
8250 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8251 if (NamespaceDecl *Namespace
8252 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8253 Namespaces.push_back(Namespace);
8254 }
8255}
8256
8257void ASTReader::ReadUndefinedButUsed(
8258 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8259 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8260 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8261 SourceLocation Loc =
8262 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8263 Undefined.insert(std::make_pair(D, Loc));
8264 }
8265}
8266
8267void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8268 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8269 Exprs) {
8270 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8271 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8272 uint64_t Count = DelayedDeleteExprs[Idx++];
8273 for (uint64_t C = 0; C < Count; ++C) {
8274 SourceLocation DeleteLoc =
8275 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8276 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8277 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8278 }
8279 }
8280}
8281
8282void ASTReader::ReadTentativeDefinitions(
8283 SmallVectorImpl<VarDecl *> &TentativeDefs) {
8284 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8285 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8286 if (Var)
8287 TentativeDefs.push_back(Var);
8288 }
8289 TentativeDefinitions.clear();
8290}
8291
8292void ASTReader::ReadUnusedFileScopedDecls(
8293 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8294 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8295 DeclaratorDecl *D
8296 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8297 if (D)
8298 Decls.push_back(D);
8299 }
8300 UnusedFileScopedDecls.clear();
8301}
8302
8303void ASTReader::ReadDelegatingConstructors(
8304 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8305 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8306 CXXConstructorDecl *D
8307 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8308 if (D)
8309 Decls.push_back(D);
8310 }
8311 DelegatingCtorDecls.clear();
8312}
8313
8314void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8315 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8316 TypedefNameDecl *D
8317 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8318 if (D)
8319 Decls.push_back(D);
8320 }
8321 ExtVectorDecls.clear();
8322}
8323
8324void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8325 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8326 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8327 ++I) {
8328 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8329 GetDecl(UnusedLocalTypedefNameCandidates[I]));
8330 if (D)
8331 Decls.insert(D);
8332 }
8333 UnusedLocalTypedefNameCandidates.clear();
8334}
8335
8336void ASTReader::ReadDeclsToCheckForDeferredDiags(
8337 llvm::SmallVector<Decl *, 4> &Decls) {
8338 for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N;
8339 ++I) {
8340 auto *D = dyn_cast_or_null<Decl>(
8341 GetDecl(DeclsToCheckForDeferredDiags[I]));
8342 if (D)
8343 Decls.push_back(D);
8344 }
8345 DeclsToCheckForDeferredDiags.clear();
8346}
8347
8348
8349void ASTReader::ReadReferencedSelectors(
8350 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8351 if (ReferencedSelectorsData.empty())
8352 return;
8353
8354 // If there are @selector references added them to its pool. This is for
8355 // implementation of -Wselector.
8356 unsigned int DataSize = ReferencedSelectorsData.size()-1;
8357 unsigned I = 0;
8358 while (I < DataSize) {
8359 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8360 SourceLocation SelLoc
8361 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8362 Sels.push_back(std::make_pair(Sel, SelLoc));
8363 }
8364 ReferencedSelectorsData.clear();
8365}
8366
8367void ASTReader::ReadWeakUndeclaredIdentifiers(
8368 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8369 if (WeakUndeclaredIdentifiers.empty())
8370 return;
8371
8372 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8373 IdentifierInfo *WeakId
8374 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8375 IdentifierInfo *AliasId
8376 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8377 SourceLocation Loc
8378 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8379 bool Used = WeakUndeclaredIdentifiers[I++];
8380 WeakInfo WI(AliasId, Loc);
8381 WI.setUsed(Used);
8382 WeakIDs.push_back(std::make_pair(WeakId, WI));
8383 }
8384 WeakUndeclaredIdentifiers.clear();
8385}
8386
8387void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8388 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8389 ExternalVTableUse VT;
8390 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8391 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8392 VT.DefinitionRequired = VTableUses[Idx++];
8393 VTables.push_back(VT);
8394 }
8395
8396 VTableUses.clear();
8397}
8398
8399void ASTReader::ReadPendingInstantiations(
8400 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8401 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8402 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8403 SourceLocation Loc
8404 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8405
8406 Pending.push_back(std::make_pair(D, Loc));
8407 }
8408 PendingInstantiations.clear();
8409}
8410
8411void ASTReader::ReadLateParsedTemplates(
8412 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8413 &LPTMap) {
8414 for (auto &LPT : LateParsedTemplates) {
8415 ModuleFile *FMod = LPT.first;
8416 RecordDataImpl &LateParsed = LPT.second;
8417 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8418 /* In loop */) {
8419 FunctionDecl *FD =
8420 cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8421
8422 auto LT = std::make_unique<LateParsedTemplate>();
8423 LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8424
8425 ModuleFile *F = getOwningModuleFile(LT->D);
8426 assert(F && "No module");
8427
8428 unsigned TokN = LateParsed[Idx++];
8429 LT->Toks.reserve(TokN);
8430 for (unsigned T = 0; T < TokN; ++T)
8431 LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8432
8433 LPTMap.insert(std::make_pair(FD, std::move(LT)));
8434 }
8435 }
8436}
8437
8438void ASTReader::LoadSelector(Selector Sel) {
8439 // It would be complicated to avoid reading the methods anyway. So don't.
8440 ReadMethodPool(Sel);
8441}
8442
8443void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8444 assert(ID && "Non-zero identifier ID required");
8445 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8446 IdentifiersLoaded[ID - 1] = II;
8447 if (DeserializationListener)
8448 DeserializationListener->IdentifierRead(ID, II);
8449}
8450
8451/// Set the globally-visible declarations associated with the given
8452/// identifier.
8453///
8454/// If the AST reader is currently in a state where the given declaration IDs
8455/// cannot safely be resolved, they are queued until it is safe to resolve
8456/// them.
8457///
8458/// \param II an IdentifierInfo that refers to one or more globally-visible
8459/// declarations.
8460///
8461/// \param DeclIDs the set of declaration IDs with the name @p II that are
8462/// visible at global scope.
8463///
8464/// \param Decls if non-null, this vector will be populated with the set of
8465/// deserialized declarations. These declarations will not be pushed into
8466/// scope.
8467void
8468ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8469 const SmallVectorImpl<uint32_t> &DeclIDs,
8470 SmallVectorImpl<Decl *> *Decls) {
8471 if (NumCurrentElementsDeserializing && !Decls) {
8472 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8473 return;
8474 }
8475
8476 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8477 if (!SemaObj) {
8478 // Queue this declaration so that it will be added to the
8479 // translation unit scope and identifier's declaration chain
8480 // once a Sema object is known.
8481 PreloadedDeclIDs.push_back(DeclIDs[I]);
8482 continue;
8483 }
8484
8485 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8486
8487 // If we're simply supposed to record the declarations, do so now.
8488 if (Decls) {
8489 Decls->push_back(D);
8490 continue;
8491 }
8492
8493 // Introduce this declaration into the translation-unit scope
8494 // and add it to the declaration chain for this identifier, so
8495 // that (unqualified) name lookup will find it.
8496 pushExternalDeclIntoScope(D, II);
8497 }
8498}
8499
8500IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8501 if (ID == 0)
8502 return nullptr;
8503
8504 if (IdentifiersLoaded.empty()) {
8505 Error("no identifier table in AST file");
8506 return nullptr;
8507 }
8508
8509 ID -= 1;
8510 if (!IdentifiersLoaded[ID]) {
8511 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8512 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8513 ModuleFile *M = I->second;
8514 unsigned Index = ID - M->BaseIdentifierID;
8515 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8516
8517 // All of the strings in the AST file are preceded by a 16-bit length.
8518 // Extract that 16-bit length to avoid having to execute strlen().
8519 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8520 // unsigned integers. This is important to avoid integer overflow when
8521 // we cast them to 'unsigned'.
8522 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8523 unsigned StrLen = (((unsigned) StrLenPtr[0])
8524 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8525 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8526 IdentifiersLoaded[ID] = &II;
8527 markIdentifierFromAST(*this, II);
8528 if (DeserializationListener)
8529 DeserializationListener->IdentifierRead(ID + 1, &II);
8530 }
8531
8532 return IdentifiersLoaded[ID];
8533}
8534
8535IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8536 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8537}
8538
8539IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8540 if (LocalID < NUM_PREDEF_IDENT_IDS)
8541 return LocalID;
8542
8543 if (!M.ModuleOffsetMap.empty())
8544 ReadModuleOffsetMap(M);
8545
8546 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8547 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8548 assert(I != M.IdentifierRemap.end()
8549 && "Invalid index into identifier index remap");
8550
8551 return LocalID + I->second;
8552}
8553
8554MacroInfo *ASTReader::getMacro(MacroID ID) {
8555 if (ID == 0)
8556 return nullptr;
8557
8558 if (MacrosLoaded.empty()) {
8559 Error("no macro table in AST file");
8560 return nullptr;
8561 }
8562
8563 ID -= NUM_PREDEF_MACRO_IDS;
8564 if (!MacrosLoaded[ID]) {
8565 GlobalMacroMapType::iterator I
8566 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8567 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8568 ModuleFile *M = I->second;
8569 unsigned Index = ID - M->BaseMacroID;
8570 MacrosLoaded[ID] =
8571 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8572
8573 if (DeserializationListener)
8574 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8575 MacrosLoaded[ID]);
8576 }
8577
8578 return MacrosLoaded[ID];
8579}
8580
8581MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8582 if (LocalID < NUM_PREDEF_MACRO_IDS)
8583 return LocalID;
8584
8585 if (!M.ModuleOffsetMap.empty())
8586 ReadModuleOffsetMap(M);
8587
8588 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8589 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8590 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8591
8592 return LocalID + I->second;
8593}
8594
8595serialization::SubmoduleID
8596ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8597 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8598 return LocalID;
8599
8600 if (!M.ModuleOffsetMap.empty())
8601 ReadModuleOffsetMap(M);
8602
8603 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8604 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8605 assert(I != M.SubmoduleRemap.end()
8606 && "Invalid index into submodule index remap");
8607
8608 return LocalID + I->second;
8609}
8610
8611Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8612 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8613 assert(GlobalID == 0 && "Unhandled global submodule ID");
8614 return nullptr;
8615 }
8616
8617 if (GlobalID > SubmodulesLoaded.size()) {
8618 Error("submodule ID out of range in AST file");
8619 return nullptr;
8620 }
8621
8622 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8623}
8624
8625Module *ASTReader::getModule(unsigned ID) {
8626 return getSubmodule(ID);
8627}
8628
8629ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8630 if (ID & 1) {
8631 // It's a module, look it up by submodule ID.
8632 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8633 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8634 } else {
8635 // It's a prefix (preamble, PCH, ...). Look it up by index.
8636 unsigned IndexFromEnd = ID >> 1;
8637 assert(IndexFromEnd && "got reference to unknown module file");
8638 return getModuleManager().pch_modules().end()[-IndexFromEnd];
8639 }
8640}
8641
8642unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8643 if (!F)
8644 return 1;
8645
8646 // For a file representing a module, use the submodule ID of the top-level
8647 // module as the file ID. For any other kind of file, the number of such
8648 // files loaded beforehand will be the same on reload.
8649 // FIXME: Is this true even if we have an explicit module file and a PCH?
8650 if (F->isModule())
8651 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8652
8653 auto PCHModules = getModuleManager().pch_modules();
8654 auto I = llvm::find(PCHModules, F);
8655 assert(I != PCHModules.end() && "emitting reference to unknown file");
8656 return (I - PCHModules.end()) << 1;
8657}
8658
8659llvm::Optional<ASTSourceDescriptor>
8660ASTReader::getSourceDescriptor(unsigned ID) {
8661 if (Module *M = getSubmodule(ID))
8662 return ASTSourceDescriptor(*M);
8663
8664 // If there is only a single PCH, return it instead.
8665 // Chained PCH are not supported.
8666 const auto &PCHChain = ModuleMgr.pch_modules();
8667 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8668 ModuleFile &MF = ModuleMgr.getPrimaryModule();
8669 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8670 StringRef FileName = llvm::sys::path::filename(MF.FileName);
8671 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8672 MF.Signature);
8673 }
8674 return None;
8675}
8676
8677ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8678 auto I = DefinitionSource.find(FD);
8679 if (I == DefinitionSource.end())
8680 return EK_ReplyHazy;
8681 return I->second ? EK_Never : EK_Always;
8682}
8683
8684Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8685 return DecodeSelector(getGlobalSelectorID(M, LocalID));
8686}
8687
8688Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8689 if (ID == 0)
8690 return Selector();
8691
8692 if (ID > SelectorsLoaded.size()) {
8693 Error("selector ID out of range in AST file");
8694 return Selector();
8695 }
8696
8697 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8698 // Load this selector from the selector table.
8699 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8700 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8701 ModuleFile &M = *I->second;
8702 ASTSelectorLookupTrait Trait(*this, M);
8703 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8704 SelectorsLoaded[ID - 1] =
8705 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8706 if (DeserializationListener)
8707 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8708 }
8709
8710 return SelectorsLoaded[ID - 1];
8711}
8712
8713Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8714 return DecodeSelector(ID);
8715}
8716
8717uint32_t ASTReader::GetNumExternalSelectors() {
8718 // ID 0 (the null selector) is considered an external selector.
8719 return getTotalNumSelectors() + 1;
8720}
8721
8722serialization::SelectorID
8723ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8724 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8725 return LocalID;
8726
8727 if (!M.ModuleOffsetMap.empty())
8728 ReadModuleOffsetMap(M);
8729
8730 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8731 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8732 assert(I != M.SelectorRemap.end()
8733 && "Invalid index into selector index remap");
8734
8735 return LocalID + I->second;
8736}
8737
8738DeclarationNameLoc
8739ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8740 DeclarationNameLoc DNLoc;
8741 switch (Name.getNameKind()) {
8742 case DeclarationName::CXXConstructorName:
8743 case DeclarationName::CXXDestructorName:
8744 case DeclarationName::CXXConversionFunctionName:
8745 DNLoc.NamedType.TInfo = readTypeSourceInfo();
8746 break;
8747
8748 case DeclarationName::CXXOperatorName:
8749 DNLoc.CXXOperatorName.BeginOpNameLoc
8750 = readSourceLocation().getRawEncoding();
8751 DNLoc.CXXOperatorName.EndOpNameLoc
8752 = readSourceLocation().getRawEncoding();
8753 break;
8754
8755 case DeclarationName::CXXLiteralOperatorName:
8756 DNLoc.CXXLiteralOperatorName.OpNameLoc
8757 = readSourceLocation().getRawEncoding();
8758 break;
8759
8760 case DeclarationName::Identifier:
8761 case DeclarationName::ObjCZeroArgSelector:
8762 case DeclarationName::ObjCOneArgSelector:
8763 case DeclarationName::ObjCMultiArgSelector:
8764 case DeclarationName::CXXUsingDirective:
8765 case DeclarationName::CXXDeductionGuideName:
8766 break;
8767 }
8768 return DNLoc;
8769}
8770
8771DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8772 DeclarationNameInfo NameInfo;
8773 NameInfo.setName(readDeclarationName());
8774 NameInfo.setLoc(readSourceLocation());
8775 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8776 return NameInfo;
8777}
8778
8779void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8780 Info.QualifierLoc = readNestedNameSpecifierLoc();
8781 unsigned NumTPLists = readInt();
8782 Info.NumTemplParamLists = NumTPLists;
8783 if (NumTPLists) {
8784 Info.TemplParamLists =
8785 new (getContext()) TemplateParameterList *[NumTPLists];
8786 for (unsigned i = 0; i != NumTPLists; ++i)
8787 Info.TemplParamLists[i] = readTemplateParameterList();
8788 }
8789}
8790
8791TemplateParameterList *
8792ASTRecordReader::readTemplateParameterList() {
8793 SourceLocation TemplateLoc = readSourceLocation();
8794 SourceLocation LAngleLoc = readSourceLocation();
8795 SourceLocation RAngleLoc = readSourceLocation();
8796
8797 unsigned NumParams = readInt();
8798 SmallVector<NamedDecl *, 16> Params;
8799 Params.reserve(NumParams);
8800 while (NumParams--)
8801 Params.push_back(readDeclAs<NamedDecl>());
8802
8803 bool HasRequiresClause = readBool();
8804 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8805
8806 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8807 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8808 return TemplateParams;
8809}
8810
8811void ASTRecordReader::readTemplateArgumentList(
8812 SmallVectorImpl<TemplateArgument> &TemplArgs,
8813 bool Canonicalize) {
8814 unsigned NumTemplateArgs = readInt();
8815 TemplArgs.reserve(NumTemplateArgs);
8816 while (NumTemplateArgs--)
8817 TemplArgs.push_back(readTemplateArgument(Canonicalize));
8818}
8819
8820/// Read a UnresolvedSet structure.
8821void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8822 unsigned NumDecls = readInt();
8823 Set.reserve(getContext(), NumDecls);
8824 while (NumDecls--) {
8825 DeclID ID = readDeclID();
8826 AccessSpecifier AS = (AccessSpecifier) readInt();
8827 Set.addLazyDecl(getContext(), ID, AS);
8828 }
8829}
8830
8831CXXBaseSpecifier
8832ASTRecordReader::readCXXBaseSpecifier() {
8833 bool isVirtual = readBool();
8834 bool isBaseOfClass = readBool();
8835 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8836 bool inheritConstructors = readBool();
8837 TypeSourceInfo *TInfo = readTypeSourceInfo();
8838 SourceRange Range = readSourceRange();
8839 SourceLocation EllipsisLoc = readSourceLocation();
8840 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8841 EllipsisLoc);
8842 Result.setInheritConstructors(inheritConstructors);
8843 return Result;
8844}
8845
8846CXXCtorInitializer **
8847ASTRecordReader::readCXXCtorInitializers() {
8848 ASTContext &Context = getContext();
8849 unsigned NumInitializers = readInt();
8850 assert(NumInitializers && "wrote ctor initializers but have no inits");
8851 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8852 for (unsigned i = 0; i != NumInitializers; ++i) {
8853 TypeSourceInfo *TInfo = nullptr;
8854 bool IsBaseVirtual = false;
8855 FieldDecl *Member = nullptr;
8856 IndirectFieldDecl *IndirectMember = nullptr;
8857
8858 CtorInitializerType Type = (CtorInitializerType) readInt();
8859 switch (Type) {
8860 case CTOR_INITIALIZER_BASE:
8861 TInfo = readTypeSourceInfo();
8862 IsBaseVirtual = readBool();
8863 break;
8864
8865 case CTOR_INITIALIZER_DELEGATING:
8866 TInfo = readTypeSourceInfo();
8867 break;
8868
8869 case CTOR_INITIALIZER_MEMBER:
8870 Member = readDeclAs<FieldDecl>();
8871 break;
8872
8873 case CTOR_INITIALIZER_INDIRECT_MEMBER:
8874 IndirectMember = readDeclAs<IndirectFieldDecl>();
8875 break;
8876 }
8877
8878 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8879 Expr *Init = readExpr();
8880 SourceLocation LParenLoc = readSourceLocation();
8881 SourceLocation RParenLoc = readSourceLocation();
8882
8883 CXXCtorInitializer *BOMInit;
8884 if (Type == CTOR_INITIALIZER_BASE)
8885 BOMInit = new (Context)
8886 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8887 RParenLoc, MemberOrEllipsisLoc);
8888 else if (Type == CTOR_INITIALIZER_DELEGATING)
8889 BOMInit = new (Context)
8890 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8891 else if (Member)
8892 BOMInit = new (Context)
8893 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8894 Init, RParenLoc);
8895 else
8896 BOMInit = new (Context)
8897 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8898 LParenLoc, Init, RParenLoc);
8899
8900 if (/*IsWritten*/readBool()) {
8901 unsigned SourceOrder = readInt();
8902 BOMInit->setSourceOrder(SourceOrder);
8903 }
8904
8905 CtorInitializers[i] = BOMInit;
8906 }
8907
8908 return CtorInitializers;
8909}
8910
8911NestedNameSpecifierLoc
8912ASTRecordReader::readNestedNameSpecifierLoc() {
8913 ASTContext &Context = getContext();
8914 unsigned N = readInt();
8915 NestedNameSpecifierLocBuilder Builder;
8916 for (unsigned I = 0; I != N; ++I) {
8917 auto Kind = readNestedNameSpecifierKind();
8918 switch (Kind) {
8919 case NestedNameSpecifier::Identifier: {
8920 IdentifierInfo *II = readIdentifier();
8921 SourceRange Range = readSourceRange();
8922 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8923 break;
8924 }
8925
8926 case NestedNameSpecifier::Namespace: {
8927 NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8928 SourceRange Range = readSourceRange();
8929 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8930 break;
8931 }
8932
8933 case NestedNameSpecifier::NamespaceAlias: {
8934 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8935 SourceRange Range = readSourceRange();
8936 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8937 break;
8938 }
8939
8940 case NestedNameSpecifier::TypeSpec:
8941 case NestedNameSpecifier::TypeSpecWithTemplate: {
8942 bool Template = readBool();
8943 TypeSourceInfo *T = readTypeSourceInfo();
8944 if (!T)
8945 return NestedNameSpecifierLoc();
8946 SourceLocation ColonColonLoc = readSourceLocation();
8947
8948 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8949 Builder.Extend(Context,
8950 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8951 T->getTypeLoc(), ColonColonLoc);
8952 break;
8953 }
8954
8955 case NestedNameSpecifier::Global: {
8956 SourceLocation ColonColonLoc = readSourceLocation();
8957 Builder.MakeGlobal(Context, ColonColonLoc);
8958 break;
8959 }
8960
8961 case NestedNameSpecifier::Super: {
8962 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8963 SourceRange Range = readSourceRange();
8964 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8965 break;
8966 }
8967 }
8968 }
8969
8970 return Builder.getWithLocInContext(Context);
8971}
8972
8973SourceRange
8974ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8975 unsigned &Idx) {
8976 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8977 SourceLocation end = ReadSourceLocation(F, Record, Idx);
8978 return SourceRange(beg, end);
8979}
8980
8981/// Read a floating-point value
8982llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
8983 return llvm::APFloat(Sem, readAPInt());
8984}
8985
8986// Read a string
8987std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8988 unsigned Len = Record[Idx++];
8989 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8990 Idx += Len;
8991 return Result;
8992}
8993
8994std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8995 unsigned &Idx) {
8996 std::string Filename = ReadString(Record, Idx);
8997 ResolveImportedPath(F, Filename);
8998 return Filename;
8999}
9000
9001std::string ASTReader::ReadPath(StringRef BaseDirectory,
9002 const RecordData &Record, unsigned &Idx) {
9003 std::string Filename = ReadString(Record, Idx);
9004 if (!BaseDirectory.empty())
9005 ResolveImportedPath(Filename, BaseDirectory);
9006 return Filename;
9007}
9008
9009VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9010 unsigned &Idx) {
9011 unsigned Major = Record[Idx++];
9012 unsigned Minor = Record[Idx++];
9013 unsigned Subminor = Record[Idx++];
9014 if (Minor == 0)
9015 return VersionTuple(Major);
9016 if (Subminor == 0)
9017 return VersionTuple(Major, Minor - 1);
9018 return VersionTuple(Major, Minor - 1, Subminor - 1);
9019}
9020
9021CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9022 const RecordData &Record,
9023 unsigned &Idx) {
9024 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9025 return CXXTemporary::Create(getContext(), Decl);
9026}
9027
9028DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9029 return Diag(CurrentImportLoc, DiagID);
9030}
9031
9032DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9033 return Diags.Report(Loc, DiagID);
9034}
9035
9036/// Retrieve the identifier table associated with the
9037/// preprocessor.
9038IdentifierTable &ASTReader::getIdentifierTable() {
9039 return PP.getIdentifierTable();
9040}
9041
9042/// Record that the given ID maps to the given switch-case
9043/// statement.
9044void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9045 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9046 "Already have a SwitchCase with this ID");
9047 (*CurrSwitchCaseStmts)[ID] = SC;
9048}
9049
9050/// Retrieve the switch-case statement with the given ID.
9051SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9052 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9053 return (*CurrSwitchCaseStmts)[ID];
9054}
9055
9056void ASTReader::ClearSwitchCaseIDs() {
9057 CurrSwitchCaseStmts->clear();
9058}
9059
9060void ASTReader::ReadComments() {
9061 ASTContext &Context = getContext();
9062 std::vector<RawComment *> Comments;
9063 for (SmallVectorImpl<std::pair<BitstreamCursor,
9064 serialization::ModuleFile *>>::iterator
9065 I = CommentsCursors.begin(),
9066 E = CommentsCursors.end();
9067 I != E; ++I) {
9068 Comments.clear();
9069 BitstreamCursor &Cursor = I->first;
9070 serialization::ModuleFile &F = *I->second;
9071 SavedStreamPosition SavedPosition(Cursor);
9072
9073 RecordData Record;
9074 while (true) {
9075 Expected<llvm::BitstreamEntry> MaybeEntry =
9076 Cursor.advanceSkippingSubblocks(
9077 BitstreamCursor::AF_DontPopBlockAtEnd);
9078 if (!MaybeEntry) {
9079 Error(MaybeEntry.takeError());
9080 return;
9081 }
9082 llvm::BitstreamEntry Entry = MaybeEntry.get();
9083
9084 switch (Entry.Kind) {
9085 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9086 case llvm::BitstreamEntry::Error:
9087 Error("malformed block record in AST file");
9088 return;
9089 case llvm::BitstreamEntry::EndBlock:
9090 goto NextCursor;
9091 case llvm::BitstreamEntry::Record:
9092 // The interesting case.
9093 break;
9094 }
9095
9096 // Read a record.
9097 Record.clear();
9098 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9099 if (!MaybeComment) {
9100 Error(MaybeComment.takeError());
9101 return;
9102 }
9103 switch ((CommentRecordTypes)MaybeComment.get()) {
9104 case COMMENTS_RAW_COMMENT: {
9105 unsigned Idx = 0;
9106 SourceRange SR = ReadSourceRange(F, Record, Idx);
9107 RawComment::CommentKind Kind =
9108 (RawComment::CommentKind) Record[Idx++];
9109 bool IsTrailingComment = Record[Idx++];
9110 bool IsAlmostTrailingComment = Record[Idx++];
9111 Comments.push_back(new (Context) RawComment(
9112 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9113 break;
9114 }
9115 }
9116 }
9117 NextCursor:
9118 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9119 FileToOffsetToComment;
9120 for (RawComment *C : Comments) {
9121 SourceLocation CommentLoc = C->getBeginLoc();
9122 if (CommentLoc.isValid()) {
9123 std::pair<FileID, unsigned> Loc =
9124 SourceMgr.getDecomposedLoc(CommentLoc);
9125 if (Loc.first.isValid())
9126 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9127 }
9128 }
9129 }
9130}
9131
9132void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9133 bool IncludeSystem, bool Complain,
9134 llvm::function_ref<void(const serialization::InputFile &IF,
9135 bool isSystem)> Visitor) {
9136 unsigned NumUserInputs = MF.NumUserInputFiles;
9137 unsigned NumInputs = MF.InputFilesLoaded.size();
9138 assert(NumUserInputs <= NumInputs);
9139 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9140 for (unsigned I = 0; I < N; ++I) {
9141 bool IsSystem = I >= NumUserInputs;
9142 InputFile IF = getInputFile(MF, I+1, Complain);
9143 Visitor(IF, IsSystem);
9144 }
9145}
9146
9147void ASTReader::visitTopLevelModuleMaps(
9148 serialization::ModuleFile &MF,
9149 llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9150 unsigned NumInputs = MF.InputFilesLoaded.size();
9151 for (unsigned I = 0; I < NumInputs; ++I) {
9152 InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9153 if (IFI.TopLevelModuleMap)
9154 // FIXME: This unnecessarily re-reads the InputFileInfo.
9155 if (auto FE = getInputFile(MF, I + 1).getFile())
9156 Visitor(FE);
9157 }
9158}
9159
9160std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9161 // If we know the owning module, use it.
9162 if (Module *M = D->getImportedOwningModule())
9163 return M->getFullModuleName();
9164
9165 // Otherwise, use the name of the top-level module the decl is within.
9166 if (ModuleFile *M = getOwningModuleFile(D))
9167 return M->ModuleName;
9168
9169 // Not from a module.
9170 return {};
9171}
9172
9173void ASTReader::finishPendingActions() {
9174 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9175 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9176 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9177 !PendingUpdateRecords.empty()) {
9178 // If any identifiers with corresponding top-level declarations have
9179 // been loaded, load those declarations now.
9180 using TopLevelDeclsMap =
9181 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9182 TopLevelDeclsMap TopLevelDecls;
9183
9184 while (!PendingIdentifierInfos.empty()) {
9185 IdentifierInfo *II = PendingIdentifierInfos.back().first;
9186 SmallVector<uint32_t, 4> DeclIDs =
9187 std::move(PendingIdentifierInfos.back().second);
9188 PendingIdentifierInfos.pop_back();
9189
9190 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9191 }
9192
9193 // Load each function type that we deferred loading because it was a
9194 // deduced type that might refer to a local type declared within itself.
9195 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9196 auto *FD = PendingFunctionTypes[I].first;
9197 FD->setType(GetType(PendingFunctionTypes[I].second));
9198
9199 // If we gave a function a deduced return type, remember that we need to
9200 // propagate that along the redeclaration chain.
9201 auto *DT = FD->getReturnType()->getContainedDeducedType();
9202 if (DT && DT->isDeduced())
9203 PendingDeducedTypeUpdates.insert(
9204 {FD->getCanonicalDecl(), FD->getReturnType()});
9205 }
9206 PendingFunctionTypes.clear();
9207
9208 // For each decl chain that we wanted to complete while deserializing, mark
9209 // it as "still needs to be completed".
9210 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9211 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9212 }
9213 PendingIncompleteDeclChains.clear();
9214
9215 // Load pending declaration chains.
9216 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9217 loadPendingDeclChain(PendingDeclChains[I].first,
9218 PendingDeclChains[I].second);
9219 PendingDeclChains.clear();
9220
9221 // Make the most recent of the top-level declarations visible.
9222 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9223 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9224 IdentifierInfo *II = TLD->first;
9225 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9226 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9227 }
9228 }
9229
9230 // Load any pending macro definitions.
9231 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9232 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9233 SmallVector<PendingMacroInfo, 2> GlobalIDs;
9234 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9235 // Initialize the macro history from chained-PCHs ahead of module imports.
9236 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9237 ++IDIdx) {
9238 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9239 if (!Info.M->isModule())
9240 resolvePendingMacro(II, Info);
9241 }
9242 // Handle module imports.
9243 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9244 ++IDIdx) {
9245 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9246 if (Info.M->isModule())
9247 resolvePendingMacro(II, Info);
9248 }
9249 }
9250 PendingMacroIDs.clear();
9251
9252 // Wire up the DeclContexts for Decls that we delayed setting until
9253 // recursive loading is completed.
9254 while (!PendingDeclContextInfos.empty()) {
9255 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9256 PendingDeclContextInfos.pop_front();
9257 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9258 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9259 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9260 }
9261
9262 // Perform any pending declaration updates.
9263 while (!PendingUpdateRecords.empty()) {
9264 auto Update = PendingUpdateRecords.pop_back_val();
9265 ReadingKindTracker ReadingKind(Read_Decl, *this);
9266 loadDeclUpdateRecords(Update);
9267 }
9268 }
9269
9270 // At this point, all update records for loaded decls are in place, so any
9271 // fake class definitions should have become real.
9272 assert(PendingFakeDefinitionData.empty() &&
9273 "faked up a class definition but never saw the real one");
9274
9275 // If we deserialized any C++ or Objective-C class definitions, any
9276 // Objective-C protocol definitions, or any redeclarable templates, make sure
9277 // that all redeclarations point to the definitions. Note that this can only
9278 // happen now, after the redeclaration chains have been fully wired.
9279 for (Decl *D : PendingDefinitions) {
9280 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9281 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9282 // Make sure that the TagType points at the definition.
9283 const_cast<TagType*>(TagT)->decl = TD;
9284 }
9285
9286 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9287 for (auto *R = getMostRecentExistingDecl(RD); R;
9288 R = R->getPreviousDecl()) {
9289 assert((R == D) ==
9290 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9291 "declaration thinks it's the definition but it isn't");
9292 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9293 }
9294 }
9295
9296 continue;
9297 }
9298
9299 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9300 // Make sure that the ObjCInterfaceType points at the definition.
9301 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9302 ->Decl = ID;
9303
9304 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9305 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9306
9307 continue;
9308 }
9309
9310 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9311 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9312 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9313
9314 continue;
9315 }
9316
9317 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9318 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9319 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9320 }
9321 PendingDefinitions.clear();
9322
9323 // Load the bodies of any functions or methods we've encountered. We do
9324 // this now (delayed) so that we can be sure that the declaration chains
9325 // have been fully wired up (hasBody relies on this).
9326 // FIXME: We shouldn't require complete redeclaration chains here.
9327 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9328 PBEnd = PendingBodies.end();
9329 PB != PBEnd; ++PB) {
9330 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9331 // For a function defined inline within a class template, force the
9332 // canonical definition to be the one inside the canonical definition of
9333 // the template. This ensures that we instantiate from a correct view
9334 // of the template.
9335 //
9336 // Sadly we can't do this more generally: we can't be sure that all
9337 // copies of an arbitrary class definition will have the same members
9338 // defined (eg, some member functions may not be instantiated, and some
9339 // special members may or may not have been implicitly defined).
9340 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9341 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9342 continue;
9343
9344 // FIXME: Check for =delete/=default?
9345 // FIXME: Complain about ODR violations here?
9346 const FunctionDecl *Defn = nullptr;
9347 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9348 FD->setLazyBody(PB->second);
9349 } else {
9350 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9351 mergeDefinitionVisibility(NonConstDefn, FD);
9352
9353 if (!FD->isLateTemplateParsed() &&
9354 !NonConstDefn->isLateTemplateParsed() &&
9355 FD->getODRHash() != NonConstDefn->getODRHash()) {
9356 if (!isa<CXXMethodDecl>(FD)) {
9357 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9358 } else if (FD->getLexicalParent()->isFileContext() &&
9359 NonConstDefn->getLexicalParent()->isFileContext()) {
9360 // Only diagnose out-of-line method definitions. If they are
9361 // in class definitions, then an error will be generated when
9362 // processing the class bodies.
9363 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9364 }
9365 }
9366 }
9367 continue;
9368 }
9369
9370 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9371 if (!getContext().getLangOpts().Modules || !MD->hasBody())
9372 MD->setLazyBody(PB->second);
9373 }
9374 PendingBodies.clear();
9375
9376 // Do some cleanup.
9377 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9378 getContext().deduplicateMergedDefinitonsFor(ND);
9379 PendingMergedDefinitionsToDeduplicate.clear();
9380}
9381
9382void ASTReader::diagnoseOdrViolations() {
9383 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9384 PendingFunctionOdrMergeFailures.empty() &&
9385 PendingEnumOdrMergeFailures.empty())
9386 return;
9387
9388 // Trigger the import of the full definition of each class that had any
9389 // odr-merging problems, so we can produce better diagnostics for them.
9390 // These updates may in turn find and diagnose some ODR failures, so take
9391 // ownership of the set first.
9392 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9393 PendingOdrMergeFailures.clear();
9394 for (auto &Merge : OdrMergeFailures) {
9395 Merge.first->buildLookup();
9396 Merge.first->decls_begin();
9397 Merge.first->bases_begin();
9398 Merge.first->vbases_begin();
9399 for (auto &RecordPair : Merge.second) {
9400 auto *RD = RecordPair.first;
9401 RD->decls_begin();
9402 RD->bases_begin();
9403 RD->vbases_begin();
9404 }
9405 }
9406
9407 // Trigger the import of functions.
9408 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9409 PendingFunctionOdrMergeFailures.clear();
9410 for (auto &Merge : FunctionOdrMergeFailures) {
9411 Merge.first->buildLookup();
9412 Merge.first->decls_begin();
9413 Merge.first->getBody();
9414 for (auto &FD : Merge.second) {
9415 FD->buildLookup();
9416 FD->decls_begin();
9417 FD->getBody();
9418 }
9419 }
9420
9421 // Trigger the import of enums.
9422 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9423 PendingEnumOdrMergeFailures.clear();
9424 for (auto &Merge : EnumOdrMergeFailures) {
9425 Merge.first->decls_begin();
9426 for (auto &Enum : Merge.second) {
9427 Enum->decls_begin();
9428 }
9429 }
9430
9431 // For each declaration from a merged context, check that the canonical
9432 // definition of that context also contains a declaration of the same
9433 // entity.
9434 //
9435 // Caution: this loop does things that might invalidate iterators into
9436 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9437 while (!PendingOdrMergeChecks.empty()) {
9438 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9439
9440 // FIXME: Skip over implicit declarations for now. This matters for things
9441 // like implicitly-declared special member functions. This isn't entirely
9442 // correct; we can end up with multiple unmerged declarations of the same
9443 // implicit entity.
9444 if (D->isImplicit())
9445 continue;
9446
9447 DeclContext *CanonDef = D->getDeclContext();
9448
9449 bool Found = false;
9450 const Decl *DCanon = D->getCanonicalDecl();
9451
9452 for (auto RI : D->redecls()) {
9453 if (RI->getLexicalDeclContext() == CanonDef) {
9454 Found = true;
9455 break;
9456 }
9457 }
9458 if (Found)
9459 continue;
9460
9461 // Quick check failed, time to do the slow thing. Note, we can't just
9462 // look up the name of D in CanonDef here, because the member that is
9463 // in CanonDef might not be found by name lookup (it might have been
9464 // replaced by a more recent declaration in the lookup table), and we
9465 // can't necessarily find it in the redeclaration chain because it might
9466 // be merely mergeable, not redeclarable.
9467 llvm::SmallVector<const NamedDecl*, 4> Candidates;
9468 for (auto *CanonMember : CanonDef->decls()) {
9469 if (CanonMember->getCanonicalDecl() == DCanon) {
9470 // This can happen if the declaration is merely mergeable and not
9471 // actually redeclarable (we looked for redeclarations earlier).
9472 //
9473 // FIXME: We should be able to detect this more efficiently, without
9474 // pulling in all of the members of CanonDef.
9475 Found = true;
9476 break;
9477 }
9478 if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9479 if (ND->getDeclName() == D->getDeclName())
9480 Candidates.push_back(ND);
9481 }
9482
9483 if (!Found) {
9484 // The AST doesn't like TagDecls becoming invalid after they've been
9485 // completed. We only really need to mark FieldDecls as invalid here.
9486 if (!isa<TagDecl>(D))
9487 D->setInvalidDecl();
9488
9489 // Ensure we don't accidentally recursively enter deserialization while
9490 // we're producing our diagnostic.
9491 Deserializing RecursionGuard(this);
9492
9493 std::string CanonDefModule =
9494 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9495 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9496 << D << getOwningModuleNameForDiagnostic(D)
9497 << CanonDef << CanonDefModule.empty() << CanonDefModule;
9498
9499 if (Candidates.empty())
9500 Diag(cast<Decl>(CanonDef)->getLocation(),
9501 diag::note_module_odr_violation_no_possible_decls) << D;
9502 else {
9503 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9504 Diag(Candidates[I]->getLocation(),
9505 diag::note_module_odr_violation_possible_decl)
9506 << Candidates[I];
9507 }
9508
9509 DiagnosedOdrMergeFailures.insert(CanonDef);
9510 }
9511 }
9512
9513 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9514 EnumOdrMergeFailures.empty())
9515 return;
9516
9517 // Ensure we don't accidentally recursively enter deserialization while
9518 // we're producing our diagnostics.
9519 Deserializing RecursionGuard(this);
9520
9521 // Common code for hashing helpers.
9522 ODRHash Hash;
9523 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9524 Hash.clear();
9525 Hash.AddQualType(Ty);
9526 return Hash.CalculateHash();
9527 };
9528
9529 auto ComputeODRHash = [&Hash](const Stmt *S) {
9530 assert(S);
9531 Hash.clear();
9532 Hash.AddStmt(S);
9533 return Hash.CalculateHash();
9534 };
9535
9536 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9537 assert(D);
9538 Hash.clear();
9539 Hash.AddSubDecl(D);
9540 return Hash.CalculateHash();
9541 };
9542
9543 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9544 Hash.clear();
9545 Hash.AddTemplateArgument(TA);
9546 return Hash.CalculateHash();
9547 };
9548
9549 auto ComputeTemplateParameterListODRHash =
9550 [&Hash](const TemplateParameterList *TPL) {
9551 assert(TPL);
9552 Hash.clear();
9553 Hash.AddTemplateParameterList(TPL);
9554 return Hash.CalculateHash();
9555 };
9556
9557 // Used with err_module_odr_violation_mismatch_decl and
9558 // note_module_odr_violation_mismatch_decl
9559 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9560 enum ODRMismatchDecl {
9561 EndOfClass,
9562 PublicSpecifer,
9563 PrivateSpecifer,
9564 ProtectedSpecifer,
9565 StaticAssert,
9566 Field,
9567 CXXMethod,
9568 TypeAlias,
9569 TypeDef,
9570 Var,
9571 Friend,
9572 FunctionTemplate,
9573 Other
9574 };
9575
9576 // Used with err_module_odr_violation_mismatch_decl_diff and
9577 // note_module_odr_violation_mismatch_decl_diff
9578 enum ODRMismatchDeclDifference {
9579 StaticAssertCondition,
9580 StaticAssertMessage,
9581 StaticAssertOnlyMessage,
9582 FieldName,
9583 FieldTypeName,
9584 FieldSingleBitField,
9585 FieldDifferentWidthBitField,
9586 FieldSingleMutable,
9587 FieldSingleInitializer,
9588 FieldDifferentInitializers,
9589 MethodName,
9590 MethodDeleted,
9591 MethodDefaulted,
9592 MethodVirtual,
9593 MethodStatic,
9594 MethodVolatile,
9595 MethodConst,
9596 MethodInline,
9597 MethodNumberParameters,
9598 MethodParameterType,
9599 MethodParameterName,
9600 MethodParameterSingleDefaultArgument,
9601 MethodParameterDifferentDefaultArgument,
9602 MethodNoTemplateArguments,
9603 MethodDifferentNumberTemplateArguments,
9604 MethodDifferentTemplateArgument,
9605 MethodSingleBody,
9606 MethodDifferentBody,
9607 TypedefName,
9608 TypedefType,
9609 VarName,
9610 VarType,
9611 VarSingleInitializer,
9612 VarDifferentInitializer,
9613 VarConstexpr,
9614 FriendTypeFunction,
9615 FriendType,
9616 FriendFunction,
9617 FunctionTemplateDifferentNumberParameters,
9618 FunctionTemplateParameterDifferentKind,
9619 FunctionTemplateParameterName,
9620 FunctionTemplateParameterSingleDefaultArgument,
9621 FunctionTemplateParameterDifferentDefaultArgument,
9622 FunctionTemplateParameterDifferentType,
9623 FunctionTemplatePackParameter,
9624 };
9625
9626 // These lambdas have the common portions of the ODR diagnostics. This
9627 // has the same return as Diag(), so addition parameters can be passed
9628 // in with operator<<
9629 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9630 SourceLocation Loc, SourceRange Range,
9631 ODRMismatchDeclDifference DiffType) {
9632 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9633 << FirstRecord << FirstModule.empty() << FirstModule << Range
9634 << DiffType;
9635 };
9636 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9637 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9638 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9639 << SecondModule << Range << DiffType;
9640 };
9641
9642 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9643 &ComputeQualTypeODRHash, &ComputeODRHash](
9644 NamedDecl *FirstRecord, StringRef FirstModule,
9645 StringRef SecondModule, FieldDecl *FirstField,
9646 FieldDecl *SecondField) {
9647 IdentifierInfo *FirstII = FirstField->getIdentifier();
9648 IdentifierInfo *SecondII = SecondField->getIdentifier();
9649 if (FirstII->getName() != SecondII->getName()) {
9650 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9651 FirstField->getSourceRange(), FieldName)
9652 << FirstII;
9653 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9654 SecondField->getSourceRange(), FieldName)
9655 << SecondII;
9656
9657 return true;
9658 }
9659
9660 assert(getContext().hasSameType(FirstField->getType(),
9661 SecondField->getType()));
9662
9663 QualType FirstType = FirstField->getType();
9664 QualType SecondType = SecondField->getType();
9665 if (ComputeQualTypeODRHash(FirstType) !=
9666 ComputeQualTypeODRHash(SecondType)) {
9667 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9668 FirstField->getSourceRange(), FieldTypeName)
9669 << FirstII << FirstType;
9670 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9671 SecondField->getSourceRange(), FieldTypeName)
9672 << SecondII << SecondType;
9673
9674 return true;
9675 }
9676
9677 const bool IsFirstBitField = FirstField->isBitField();
9678 const bool IsSecondBitField = SecondField->isBitField();
9679 if (IsFirstBitField != IsSecondBitField) {
9680 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9681 FirstField->getSourceRange(), FieldSingleBitField)
9682 << FirstII << IsFirstBitField;
9683 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9684 SecondField->getSourceRange(), FieldSingleBitField)
9685 << SecondII << IsSecondBitField;
9686 return true;
9687 }
9688
9689 if (IsFirstBitField && IsSecondBitField) {
9690 unsigned FirstBitWidthHash =
9691 ComputeODRHash(FirstField->getBitWidth());
9692 unsigned SecondBitWidthHash =
9693 ComputeODRHash(SecondField->getBitWidth());
9694 if (FirstBitWidthHash != SecondBitWidthHash) {
9695 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9696 FirstField->getSourceRange(),
9697 FieldDifferentWidthBitField)
9698 << FirstII << FirstField->getBitWidth()->getSourceRange();
9699 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9700 SecondField->getSourceRange(),
9701 FieldDifferentWidthBitField)
9702 << SecondII << SecondField->getBitWidth()->getSourceRange();
9703 return true;
9704 }
9705 }
9706
9707 if (!PP.getLangOpts().CPlusPlus)
9708 return false;
9709
9710 const bool IsFirstMutable = FirstField->isMutable();
9711 const bool IsSecondMutable = SecondField->isMutable();
9712 if (IsFirstMutable != IsSecondMutable) {
9713 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9714 FirstField->getSourceRange(), FieldSingleMutable)
9715 << FirstII << IsFirstMutable;
9716 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9717 SecondField->getSourceRange(), FieldSingleMutable)
9718 << SecondII << IsSecondMutable;
9719 return true;
9720 }
9721
9722 const Expr *FirstInitializer = FirstField->getInClassInitializer();
9723 const Expr *SecondInitializer = SecondField->getInClassInitializer();
9724 if ((!FirstInitializer && SecondInitializer) ||
9725 (FirstInitializer && !SecondInitializer)) {
9726 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9727 FirstField->getSourceRange(), FieldSingleInitializer)
9728 << FirstII << (FirstInitializer != nullptr);
9729 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9730 SecondField->getSourceRange(), FieldSingleInitializer)
9731 << SecondII << (SecondInitializer != nullptr);
9732 return true;
9733 }
9734
9735 if (FirstInitializer && SecondInitializer) {
9736 unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9737 unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9738 if (FirstInitHash != SecondInitHash) {
9739 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9740 FirstField->getSourceRange(),
9741 FieldDifferentInitializers)
9742 << FirstII << FirstInitializer->getSourceRange();
9743 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9744 SecondField->getSourceRange(),
9745 FieldDifferentInitializers)
9746 << SecondII << SecondInitializer->getSourceRange();
9747 return true;
9748 }
9749 }
9750
9751 return false;
9752 };
9753
9754 auto ODRDiagTypeDefOrAlias =
9755 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9756 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9757 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9758 bool IsTypeAlias) {
9759 auto FirstName = FirstTD->getDeclName();
9760 auto SecondName = SecondTD->getDeclName();
9761 if (FirstName != SecondName) {
9762 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9763 FirstTD->getSourceRange(), TypedefName)
9764 << IsTypeAlias << FirstName;
9765 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9766 SecondTD->getSourceRange(), TypedefName)
9767 << IsTypeAlias << SecondName;
9768 return true;
9769 }
9770
9771 QualType FirstType = FirstTD->getUnderlyingType();
9772 QualType SecondType = SecondTD->getUnderlyingType();
9773 if (ComputeQualTypeODRHash(FirstType) !=
9774 ComputeQualTypeODRHash(SecondType)) {
9775 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9776 FirstTD->getSourceRange(), TypedefType)
9777 << IsTypeAlias << FirstName << FirstType;
9778 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9779 SecondTD->getSourceRange(), TypedefType)
9780 << IsTypeAlias << SecondName << SecondType;
9781 return true;
9782 }
9783
9784 return false;
9785 };
9786
9787 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9788 &ComputeQualTypeODRHash, &ComputeODRHash,
9789 this](NamedDecl *FirstRecord, StringRef FirstModule,
9790 StringRef SecondModule, VarDecl *FirstVD,
9791 VarDecl *SecondVD) {
9792 auto FirstName = FirstVD->getDeclName();
9793 auto SecondName = SecondVD->getDeclName();
9794 if (FirstName != SecondName) {
9795 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9796 FirstVD->getSourceRange(), VarName)
9797 << FirstName;
9798 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9799 SecondVD->getSourceRange(), VarName)
9800 << SecondName;
9801 return true;
9802 }
9803
9804 QualType FirstType = FirstVD->getType();
9805 QualType SecondType = SecondVD->getType();
9806 if (ComputeQualTypeODRHash(FirstType) !=
9807 ComputeQualTypeODRHash(SecondType)) {
9808 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9809 FirstVD->getSourceRange(), VarType)
9810 << FirstName << FirstType;
9811 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9812 SecondVD->getSourceRange(), VarType)
9813 << SecondName << SecondType;
9814 return true;
9815 }
9816
9817 if (!PP.getLangOpts().CPlusPlus)
9818 return false;
9819
9820 const Expr *FirstInit = FirstVD->getInit();
9821 const Expr *SecondInit = SecondVD->getInit();
9822 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9823 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9824 FirstVD->getSourceRange(), VarSingleInitializer)
9825 << FirstName << (FirstInit == nullptr)
9826 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9827 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9828 SecondVD->getSourceRange(), VarSingleInitializer)
9829 << SecondName << (SecondInit == nullptr)
9830 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9831 return true;
9832 }
9833
9834 if (FirstInit && SecondInit &&
9835 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9836 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9837 FirstVD->getSourceRange(), VarDifferentInitializer)
9838 << FirstName << FirstInit->getSourceRange();
9839 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9840 SecondVD->getSourceRange(), VarDifferentInitializer)
9841 << SecondName << SecondInit->getSourceRange();
9842 return true;
9843 }
9844
9845 const bool FirstIsConstexpr = FirstVD->isConstexpr();
9846 const bool SecondIsConstexpr = SecondVD->isConstexpr();
9847 if (FirstIsConstexpr != SecondIsConstexpr) {
9848 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9849 FirstVD->getSourceRange(), VarConstexpr)
9850 << FirstName << FirstIsConstexpr;
9851 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9852 SecondVD->getSourceRange(), VarConstexpr)
9853 << SecondName << SecondIsConstexpr;
9854 return true;
9855 }
9856 return false;
9857 };
9858
9859 auto DifferenceSelector = [](Decl *D) {
9860 assert(D && "valid Decl required");
9861 switch (D->getKind()) {
9862 default:
9863 return Other;
9864 case Decl::AccessSpec:
9865 switch (D->getAccess()) {
9866 case AS_public:
9867 return PublicSpecifer;
9868 case AS_private:
9869 return PrivateSpecifer;
9870 case AS_protected:
9871 return ProtectedSpecifer;
9872 case AS_none:
9873 break;
9874 }
9875 llvm_unreachable("Invalid access specifier");
9876 case Decl::StaticAssert:
9877 return StaticAssert;
9878 case Decl::Field:
9879 return Field;
9880 case Decl::CXXMethod:
9881 case Decl::CXXConstructor:
9882 case Decl::CXXDestructor:
9883 return CXXMethod;
9884 case Decl::TypeAlias:
9885 return TypeAlias;
9886 case Decl::Typedef:
9887 return TypeDef;
9888 case Decl::Var:
9889 return Var;
9890 case Decl::Friend:
9891 return Friend;
9892 case Decl::FunctionTemplate:
9893 return FunctionTemplate;
9894 }
9895 };
9896
9897 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9898 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9899 RecordDecl *Record,
9900 const DeclContext *DC) {
9901 for (auto *D : Record->decls()) {
9902 if (!ODRHash::isDeclToBeProcessed(D, DC))
9903 continue;
9904 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9905 }
9906 };
9907
9908 struct DiffResult {
9909 Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9910 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9911 };
9912
9913 // If there is a diagnoseable difference, FirstDiffType and
9914 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9915 // filled in if not EndOfClass.
9916 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9917 DeclHashes &SecondHashes) {
9918 DiffResult DR;
9919 auto FirstIt = FirstHashes.begin();
9920 auto SecondIt = SecondHashes.begin();
9921 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9922 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9923 FirstIt->second == SecondIt->second) {
9924 ++FirstIt;
9925 ++SecondIt;
9926 continue;
9927 }
9928
9929 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9930 DR.SecondDecl =
9931 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9932
9933 DR.FirstDiffType =
9934 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9935 DR.SecondDiffType =
9936 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9937 return DR;
9938 }
9939 return DR;
9940 };
9941
9942 // Use this to diagnose that an unexpected Decl was encountered
9943 // or no difference was detected. This causes a generic error
9944 // message to be emitted.
9945 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9946 StringRef FirstModule,
9947 NamedDecl *SecondRecord,
9948 StringRef SecondModule) {
9949 Diag(FirstRecord->getLocation(),
9950 diag::err_module_odr_violation_different_definitions)
9951 << FirstRecord << FirstModule.empty() << FirstModule;
9952
9953 if (DR.FirstDecl) {
9954 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9955 << FirstRecord << DR.FirstDecl->getSourceRange();
9956 }
9957
9958 Diag(SecondRecord->getLocation(),
9959 diag::note_module_odr_violation_different_definitions)
9960 << SecondModule;
9961
9962 if (DR.SecondDecl) {
9963 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9964 << DR.SecondDecl->getSourceRange();
9965 }
9966 };
9967
9968 auto DiagnoseODRMismatch =
9969 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9970 NamedDecl *SecondRecord, StringRef SecondModule) {
9971 SourceLocation FirstLoc;
9972 SourceRange FirstRange;
9973 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9974 if (DR.FirstDiffType == EndOfClass && FirstTag) {
9975 FirstLoc = FirstTag->getBraceRange().getEnd();
9976 } else {
9977 FirstLoc = DR.FirstDecl->getLocation();
9978 FirstRange = DR.FirstDecl->getSourceRange();
9979 }
9980 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
9981 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
9982 << DR.FirstDiffType;
9983
9984 SourceLocation SecondLoc;
9985 SourceRange SecondRange;
9986 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
9987 if (DR.SecondDiffType == EndOfClass && SecondTag) {
9988 SecondLoc = SecondTag->getBraceRange().getEnd();
9989 } else {
9990 SecondLoc = DR.SecondDecl->getLocation();
9991 SecondRange = DR.SecondDecl->getSourceRange();
9992 }
9993 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
9994 << SecondModule << SecondRange << DR.SecondDiffType;
9995 };
9996
9997 // Issue any pending ODR-failure diagnostics.
9998 for (auto &Merge : OdrMergeFailures) {
9999 // If we've already pointed out a specific problem with this class, don't
10000 // bother issuing a general "something's different" diagnostic.
10001 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10002 continue;
10003
10004 bool Diagnosed = false;
10005 CXXRecordDecl *FirstRecord = Merge.first;
10006 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10007 for (auto &RecordPair : Merge.second) {
10008 CXXRecordDecl *SecondRecord = RecordPair.first;
10009 // Multiple different declarations got merged together; tell the user
10010 // where they came from.
10011 if (FirstRecord == SecondRecord)
10012 continue;
10013
10014 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10015
10016 auto *FirstDD = FirstRecord->DefinitionData;
10017 auto *SecondDD = RecordPair.second;
10018
10019 assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10020
10021 // Diagnostics from DefinitionData are emitted here.
10022 if (FirstDD != SecondDD) {
10023 enum ODRDefinitionDataDifference {
10024 NumBases,
10025 NumVBases,
10026 BaseType,
10027 BaseVirtual,
10028 BaseAccess,
10029 };
10030 auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10031 this](SourceLocation Loc, SourceRange Range,
10032 ODRDefinitionDataDifference DiffType) {
10033 return Diag(Loc, diag::err_module_odr_violation_definition_data)
10034 << FirstRecord << FirstModule.empty() << FirstModule << Range
10035 << DiffType;
10036 };
10037 auto ODRDiagBaseNote = [&SecondModule,
10038 this](SourceLocation Loc, SourceRange Range,
10039 ODRDefinitionDataDifference DiffType) {
10040 return Diag(Loc, diag::note_module_odr_violation_definition_data)
10041 << SecondModule << Range << DiffType;
10042 };
10043
10044 unsigned FirstNumBases = FirstDD->NumBases;
10045 unsigned FirstNumVBases = FirstDD->NumVBases;
10046 unsigned SecondNumBases = SecondDD->NumBases;
10047 unsigned SecondNumVBases = SecondDD->NumVBases;
10048
10049 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10050 unsigned NumBases = DD->NumBases;
10051 if (NumBases == 0) return SourceRange();
10052 auto bases = DD->bases();
10053 return SourceRange(bases[0].getBeginLoc(),
10054 bases[NumBases - 1].getEndLoc());
10055 };
10056
10057 if (FirstNumBases != SecondNumBases) {
10058 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10059 NumBases)
10060 << FirstNumBases;
10061 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10062 NumBases)
10063 << SecondNumBases;
10064 Diagnosed = true;
10065 break;
10066 }
10067
10068 if (FirstNumVBases != SecondNumVBases) {
10069 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10070 NumVBases)
10071 << FirstNumVBases;
10072 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10073 NumVBases)
10074 << SecondNumVBases;
10075 Diagnosed = true;
10076 break;
10077 }
10078
10079 auto FirstBases = FirstDD->bases();
10080 auto SecondBases = SecondDD->bases();
10081 unsigned i = 0;
10082 for (i = 0; i < FirstNumBases; ++i) {
10083 auto FirstBase = FirstBases[i];
10084 auto SecondBase = SecondBases[i];
10085 if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10086 ComputeQualTypeODRHash(SecondBase.getType())) {
10087 ODRDiagBaseError(FirstRecord->getLocation(),
10088 FirstBase.getSourceRange(), BaseType)
10089 << (i + 1) << FirstBase.getType();
10090 ODRDiagBaseNote(SecondRecord->getLocation(),
10091 SecondBase.getSourceRange(), BaseType)
10092 << (i + 1) << SecondBase.getType();
10093 break;
10094 }
10095
10096 if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10097 ODRDiagBaseError(FirstRecord->getLocation(),
10098 FirstBase.getSourceRange(), BaseVirtual)
10099 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10100 ODRDiagBaseNote(SecondRecord->getLocation(),
10101 SecondBase.getSourceRange(), BaseVirtual)
10102 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10103 break;
10104 }
10105
10106 if (FirstBase.getAccessSpecifierAsWritten() !=
10107 SecondBase.getAccessSpecifierAsWritten()) {
10108 ODRDiagBaseError(FirstRecord->getLocation(),
10109 FirstBase.getSourceRange(), BaseAccess)
10110 << (i + 1) << FirstBase.getType()
10111 << (int)FirstBase.getAccessSpecifierAsWritten();
10112 ODRDiagBaseNote(SecondRecord->getLocation(),
10113 SecondBase.getSourceRange(), BaseAccess)
10114 << (i + 1) << SecondBase.getType()
10115 << (int)SecondBase.getAccessSpecifierAsWritten();
10116 break;
10117 }
10118 }
10119
10120 if (i != FirstNumBases) {
10121 Diagnosed = true;
10122 break;
10123 }
10124 }
10125
10126 const ClassTemplateDecl *FirstTemplate =
10127 FirstRecord->getDescribedClassTemplate();
10128 const ClassTemplateDecl *SecondTemplate =
10129 SecondRecord->getDescribedClassTemplate();
10130
10131 assert(!FirstTemplate == !SecondTemplate &&
10132 "Both pointers should be null or non-null");
10133
10134 enum ODRTemplateDifference {
10135 ParamEmptyName,
10136 ParamName,
10137 ParamSingleDefaultArgument,
10138 ParamDifferentDefaultArgument,
10139 };
10140
10141 if (FirstTemplate && SecondTemplate) {
10142 DeclHashes FirstTemplateHashes;
10143 DeclHashes SecondTemplateHashes;
10144
10145 auto PopulateTemplateParameterHashs =
10146 [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10147 const ClassTemplateDecl *TD) {
10148 for (auto *D : TD->getTemplateParameters()->asArray()) {
10149 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10150 }
10151 };
10152
10153 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10154 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10155
10156 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10157 "Number of template parameters should be equal.");
10158
10159 auto FirstIt = FirstTemplateHashes.begin();
10160 auto FirstEnd = FirstTemplateHashes.end();
10161 auto SecondIt = SecondTemplateHashes.begin();
10162 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10163 if (FirstIt->second == SecondIt->second)
10164 continue;
10165
10166 auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10167 SourceLocation Loc, SourceRange Range,
10168 ODRTemplateDifference DiffType) {
10169 return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10170 << FirstRecord << FirstModule.empty() << FirstModule << Range
10171 << DiffType;
10172 };
10173 auto ODRDiagTemplateNote = [&SecondModule, this](
10174 SourceLocation Loc, SourceRange Range,
10175 ODRTemplateDifference DiffType) {
10176 return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10177 << SecondModule << Range << DiffType;
10178 };
10179
10180 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10181 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10182
10183 assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10184 "Parameter Decl's should be the same kind.");
10185
10186 DeclarationName FirstName = FirstDecl->getDeclName();
10187 DeclarationName SecondName = SecondDecl->getDeclName();
10188
10189 if (FirstName != SecondName) {
10190 const bool FirstNameEmpty =
10191 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10192 const bool SecondNameEmpty =
10193 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10194 assert((!FirstNameEmpty || !SecondNameEmpty) &&
10195 "Both template parameters cannot be unnamed.");
10196 ODRDiagTemplateError(FirstDecl->getLocation(),
10197 FirstDecl->getSourceRange(),
10198 FirstNameEmpty ? ParamEmptyName : ParamName)
10199 << FirstName;
10200 ODRDiagTemplateNote(SecondDecl->getLocation(),
10201 SecondDecl->getSourceRange(),
10202 SecondNameEmpty ? ParamEmptyName : ParamName)
10203 << SecondName;
10204 break;
10205 }
10206
10207 switch (FirstDecl->getKind()) {
10208 default:
10209 llvm_unreachable("Invalid template parameter type.");
10210 case Decl::TemplateTypeParm: {
10211 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10212 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10213 const bool HasFirstDefaultArgument =
10214 FirstParam->hasDefaultArgument() &&
10215 !FirstParam->defaultArgumentWasInherited();
10216 const bool HasSecondDefaultArgument =
10217 SecondParam->hasDefaultArgument() &&
10218 !SecondParam->defaultArgumentWasInherited();
10219
10220 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10221 ODRDiagTemplateError(FirstDecl->getLocation(),
10222 FirstDecl->getSourceRange(),
10223 ParamSingleDefaultArgument)
10224 << HasFirstDefaultArgument;
10225 ODRDiagTemplateNote(SecondDecl->getLocation(),
10226 SecondDecl->getSourceRange(),
10227 ParamSingleDefaultArgument)
10228 << HasSecondDefaultArgument;
10229 break;
10230 }
10231
10232 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10233 "Expecting default arguments.");
10234
10235 ODRDiagTemplateError(FirstDecl->getLocation(),
10236 FirstDecl->getSourceRange(),
10237 ParamDifferentDefaultArgument);
10238 ODRDiagTemplateNote(SecondDecl->getLocation(),
10239 SecondDecl->getSourceRange(),
10240 ParamDifferentDefaultArgument);
10241
10242 break;
10243 }
10244 case Decl::NonTypeTemplateParm: {
10245 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10246 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10247 const bool HasFirstDefaultArgument =
10248 FirstParam->hasDefaultArgument() &&
10249 !FirstParam->defaultArgumentWasInherited();
10250 const bool HasSecondDefaultArgument =
10251 SecondParam->hasDefaultArgument() &&
10252 !SecondParam->defaultArgumentWasInherited();
10253
10254 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10255 ODRDiagTemplateError(FirstDecl->getLocation(),
10256 FirstDecl->getSourceRange(),
10257 ParamSingleDefaultArgument)
10258 << HasFirstDefaultArgument;
10259 ODRDiagTemplateNote(SecondDecl->getLocation(),
10260 SecondDecl->getSourceRange(),
10261 ParamSingleDefaultArgument)
10262 << HasSecondDefaultArgument;
10263 break;
10264 }
10265
10266 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10267 "Expecting default arguments.");
10268
10269 ODRDiagTemplateError(FirstDecl->getLocation(),
10270 FirstDecl->getSourceRange(),
10271 ParamDifferentDefaultArgument);
10272 ODRDiagTemplateNote(SecondDecl->getLocation(),
10273 SecondDecl->getSourceRange(),
10274 ParamDifferentDefaultArgument);
10275
10276 break;
10277 }
10278 case Decl::TemplateTemplateParm: {
10279 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10280 const auto *SecondParam =
10281 cast<TemplateTemplateParmDecl>(SecondDecl);
10282 const bool HasFirstDefaultArgument =
10283 FirstParam->hasDefaultArgument() &&
10284 !FirstParam->defaultArgumentWasInherited();
10285 const bool HasSecondDefaultArgument =
10286 SecondParam->hasDefaultArgument() &&
10287 !SecondParam->defaultArgumentWasInherited();
10288
10289 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10290 ODRDiagTemplateError(FirstDecl->getLocation(),
10291 FirstDecl->getSourceRange(),
10292 ParamSingleDefaultArgument)
10293 << HasFirstDefaultArgument;
10294 ODRDiagTemplateNote(SecondDecl->getLocation(),
10295 SecondDecl->getSourceRange(),
10296 ParamSingleDefaultArgument)
10297 << HasSecondDefaultArgument;
10298 break;
10299 }
10300
10301 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10302 "Expecting default arguments.");
10303
10304 ODRDiagTemplateError(FirstDecl->getLocation(),
10305 FirstDecl->getSourceRange(),
10306 ParamDifferentDefaultArgument);
10307 ODRDiagTemplateNote(SecondDecl->getLocation(),
10308 SecondDecl->getSourceRange(),
10309 ParamDifferentDefaultArgument);
10310
10311 break;
10312 }
10313 }
10314
10315 break;
10316 }
10317
10318 if (FirstIt != FirstEnd) {
10319 Diagnosed = true;
10320 break;
10321 }
10322 }
10323
10324 DeclHashes FirstHashes;
10325 DeclHashes SecondHashes;
10326 const DeclContext *DC = FirstRecord;
10327 PopulateHashes(FirstHashes, FirstRecord, DC);
10328 PopulateHashes(SecondHashes, SecondRecord, DC);
10329
10330 auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10331 ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10332 ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10333 Decl *FirstDecl = DR.FirstDecl;
10334 Decl *SecondDecl = DR.SecondDecl;
10335
10336 if (FirstDiffType == Other || SecondDiffType == Other) {
10337 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10338 SecondModule);
10339 Diagnosed = true;
10340 break;
10341 }
10342
10343 if (FirstDiffType != SecondDiffType) {
10344 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10345 SecondModule);
10346 Diagnosed = true;
10347 break;
10348 }
10349
10350 assert(FirstDiffType == SecondDiffType);
10351
10352 switch (FirstDiffType) {
10353 case Other:
10354 case EndOfClass:
10355 case PublicSpecifer:
10356 case PrivateSpecifer:
10357 case ProtectedSpecifer:
10358 llvm_unreachable("Invalid diff type");
10359
10360 case StaticAssert: {
10361 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10362 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10363
10364 Expr *FirstExpr = FirstSA->getAssertExpr();
10365 Expr *SecondExpr = SecondSA->getAssertExpr();
10366 unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10367 unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10368 if (FirstODRHash != SecondODRHash) {
10369 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10370 FirstExpr->getSourceRange(), StaticAssertCondition);
10371 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10372 SecondExpr->getSourceRange(), StaticAssertCondition);
10373 Diagnosed = true;
10374 break;
10375 }
10376
10377 StringLiteral *FirstStr = FirstSA->getMessage();
10378 StringLiteral *SecondStr = SecondSA->getMessage();
10379 assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10380 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10381 SourceLocation FirstLoc, SecondLoc;
10382 SourceRange FirstRange, SecondRange;
10383 if (FirstStr) {
10384 FirstLoc = FirstStr->getBeginLoc();
10385 FirstRange = FirstStr->getSourceRange();
10386 } else {
10387 FirstLoc = FirstSA->getBeginLoc();
10388 FirstRange = FirstSA->getSourceRange();
10389 }
10390 if (SecondStr) {
10391 SecondLoc = SecondStr->getBeginLoc();
10392 SecondRange = SecondStr->getSourceRange();
10393 } else {
10394 SecondLoc = SecondSA->getBeginLoc();
10395 SecondRange = SecondSA->getSourceRange();
10396 }
10397 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10398 StaticAssertOnlyMessage)
10399 << (FirstStr == nullptr);
10400 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10401 StaticAssertOnlyMessage)
10402 << (SecondStr == nullptr);
10403 Diagnosed = true;
10404 break;
10405 }
10406
10407 if (FirstStr && SecondStr &&
10408 FirstStr->getString() != SecondStr->getString()) {
10409 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10410 FirstStr->getSourceRange(), StaticAssertMessage);
10411 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10412 SecondStr->getSourceRange(), StaticAssertMessage);
10413 Diagnosed = true;
10414 break;
10415 }
10416 break;
10417 }
10418 case Field: {
10419 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10420 cast<FieldDecl>(FirstDecl),
10421 cast<FieldDecl>(SecondDecl));
10422 break;
10423 }
10424 case CXXMethod: {
10425 enum {
10426 DiagMethod,
10427 DiagConstructor,
10428 DiagDestructor,
10429 } FirstMethodType,
10430 SecondMethodType;
10431 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10432 if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10433 if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10434 return DiagMethod;
10435 };
10436 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10437 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10438 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10439 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10440 auto FirstName = FirstMethod->getDeclName();
10441 auto SecondName = SecondMethod->getDeclName();
10442 if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10443 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10444 FirstMethod->getSourceRange(), MethodName)
10445 << FirstMethodType << FirstName;
10446 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10447 SecondMethod->getSourceRange(), MethodName)
10448 << SecondMethodType << SecondName;
10449
10450 Diagnosed = true;
10451 break;
10452 }
10453
10454 const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10455 const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10456 if (FirstDeleted != SecondDeleted) {
10457 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10458 FirstMethod->getSourceRange(), MethodDeleted)
10459 << FirstMethodType << FirstName << FirstDeleted;
10460
10461 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10462 SecondMethod->getSourceRange(), MethodDeleted)
10463 << SecondMethodType << SecondName << SecondDeleted;
10464 Diagnosed = true;
10465 break;
10466 }
10467
10468 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10469 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10470 if (FirstDefaulted != SecondDefaulted) {
10471 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10472 FirstMethod->getSourceRange(), MethodDefaulted)
10473 << FirstMethodType << FirstName << FirstDefaulted;
10474
10475 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10476 SecondMethod->getSourceRange(), MethodDefaulted)
10477 << SecondMethodType << SecondName << SecondDefaulted;
10478 Diagnosed = true;
10479 break;
10480 }
10481
10482 const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10483 const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10484 const bool FirstPure = FirstMethod->isPure();
10485 const bool SecondPure = SecondMethod->isPure();
10486 if ((FirstVirtual || SecondVirtual) &&
10487 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10488 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10489 FirstMethod->getSourceRange(), MethodVirtual)
10490 << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10491 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10492 SecondMethod->getSourceRange(), MethodVirtual)
10493 << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10494 Diagnosed = true;
10495 break;
10496 }
10497
10498 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging,
10499 // FirstDecl is the canonical Decl of SecondDecl, so the storage
10500 // class needs to be checked instead.
10501 const auto FirstStorage = FirstMethod->getStorageClass();
10502 const auto SecondStorage = SecondMethod->getStorageClass();
10503 const bool FirstStatic = FirstStorage == SC_Static;
10504 const bool SecondStatic = SecondStorage == SC_Static;
10505 if (FirstStatic != SecondStatic) {
10506 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10507 FirstMethod->getSourceRange(), MethodStatic)
10508 << FirstMethodType << FirstName << FirstStatic;
10509 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10510 SecondMethod->getSourceRange(), MethodStatic)
10511 << SecondMethodType << SecondName << SecondStatic;
10512 Diagnosed = true;
10513 break;
10514 }
10515
10516 const bool FirstVolatile = FirstMethod->isVolatile();
10517 const bool SecondVolatile = SecondMethod->isVolatile();
10518 if (FirstVolatile != SecondVolatile) {
10519 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10520 FirstMethod->getSourceRange(), MethodVolatile)
10521 << FirstMethodType << FirstName << FirstVolatile;
10522 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10523 SecondMethod->getSourceRange(), MethodVolatile)
10524 << SecondMethodType << SecondName << SecondVolatile;
10525 Diagnosed = true;
10526 break;
10527 }
10528
10529 const bool FirstConst = FirstMethod->isConst();
10530 const bool SecondConst = SecondMethod->isConst();
10531 if (FirstConst != SecondConst) {
10532 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10533 FirstMethod->getSourceRange(), MethodConst)
10534 << FirstMethodType << FirstName << FirstConst;
10535 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10536 SecondMethod->getSourceRange(), MethodConst)
10537 << SecondMethodType << SecondName << SecondConst;
10538 Diagnosed = true;
10539 break;
10540 }
10541
10542 const bool FirstInline = FirstMethod->isInlineSpecified();
10543 const bool SecondInline = SecondMethod->isInlineSpecified();
10544 if (FirstInline != SecondInline) {
10545 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10546 FirstMethod->getSourceRange(), MethodInline)
10547 << FirstMethodType << FirstName << FirstInline;
10548 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10549 SecondMethod->getSourceRange(), MethodInline)
10550 << SecondMethodType << SecondName << SecondInline;
10551 Diagnosed = true;
10552 break;
10553 }
10554
10555 const unsigned FirstNumParameters = FirstMethod->param_size();
10556 const unsigned SecondNumParameters = SecondMethod->param_size();
10557 if (FirstNumParameters != SecondNumParameters) {
10558 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10559 FirstMethod->getSourceRange(),
10560 MethodNumberParameters)
10561 << FirstMethodType << FirstName << FirstNumParameters;
10562 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10563 SecondMethod->getSourceRange(),
10564 MethodNumberParameters)
10565 << SecondMethodType << SecondName << SecondNumParameters;
10566 Diagnosed = true;
10567 break;
10568 }
10569
10570 // Need this status boolean to know when break out of the switch.
10571 bool ParameterMismatch = false;
10572 for (unsigned I = 0; I < FirstNumParameters; ++I) {
10573 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10574 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10575
10576 QualType FirstParamType = FirstParam->getType();
10577 QualType SecondParamType = SecondParam->getType();
10578 if (FirstParamType != SecondParamType &&
10579 ComputeQualTypeODRHash(FirstParamType) !=
10580 ComputeQualTypeODRHash(SecondParamType)) {
10581 if (const DecayedType *ParamDecayedType =
10582 FirstParamType->getAs<DecayedType>()) {
10583 ODRDiagDeclError(
10584 FirstRecord, FirstModule, FirstMethod->getLocation(),
10585 FirstMethod->getSourceRange(), MethodParameterType)
10586 << FirstMethodType << FirstName << (I + 1) << FirstParamType
10587 << true << ParamDecayedType->getOriginalType();
10588 } else {
10589 ODRDiagDeclError(
10590 FirstRecord, FirstModule, FirstMethod->getLocation(),
10591 FirstMethod->getSourceRange(), MethodParameterType)
10592 << FirstMethodType << FirstName << (I + 1) << FirstParamType
10593 << false;
10594 }
10595
10596 if (const DecayedType *ParamDecayedType =
10597 SecondParamType->getAs<DecayedType>()) {
10598 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10599 SecondMethod->getSourceRange(),
10600 MethodParameterType)
10601 << SecondMethodType << SecondName << (I + 1)
10602 << SecondParamType << true
10603 << ParamDecayedType->getOriginalType();
10604 } else {
10605 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10606 SecondMethod->getSourceRange(),
10607 MethodParameterType)
10608 << SecondMethodType << SecondName << (I + 1)
10609 << SecondParamType << false;
10610 }
10611 ParameterMismatch = true;
10612 break;
10613 }
10614
10615 DeclarationName FirstParamName = FirstParam->getDeclName();
10616 DeclarationName SecondParamName = SecondParam->getDeclName();
10617 if (FirstParamName != SecondParamName) {
10618 ODRDiagDeclError(FirstRecord, FirstModule,
10619 FirstMethod->getLocation(),
10620 FirstMethod->getSourceRange(), MethodParameterName)
10621 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10622 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10623 SecondMethod->getSourceRange(), MethodParameterName)
10624 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10625 ParameterMismatch = true;
10626 break;
10627 }
10628
10629 const Expr *FirstInit = FirstParam->getInit();
10630 const Expr *SecondInit = SecondParam->getInit();
10631 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10632 ODRDiagDeclError(FirstRecord, FirstModule,
10633 FirstMethod->getLocation(),
10634 FirstMethod->getSourceRange(),
10635 MethodParameterSingleDefaultArgument)
10636 << FirstMethodType << FirstName << (I + 1)
10637 << (FirstInit == nullptr)
10638 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10639 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10640 SecondMethod->getSourceRange(),
10641 MethodParameterSingleDefaultArgument)
10642 << SecondMethodType << SecondName << (I + 1)
10643 << (SecondInit == nullptr)
10644 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10645 ParameterMismatch = true;
10646 break;
10647 }
10648
10649 if (FirstInit && SecondInit &&
10650 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10651 ODRDiagDeclError(FirstRecord, FirstModule,
10652 FirstMethod->getLocation(),
10653 FirstMethod->getSourceRange(),
10654 MethodParameterDifferentDefaultArgument)
10655 << FirstMethodType << FirstName << (I + 1)
10656 << FirstInit->getSourceRange();
10657 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10658 SecondMethod->getSourceRange(),
10659 MethodParameterDifferentDefaultArgument)
10660 << SecondMethodType << SecondName << (I + 1)
10661 << SecondInit->getSourceRange();
10662 ParameterMismatch = true;
10663 break;
10664
10665 }
10666 }
10667
10668 if (ParameterMismatch) {
10669 Diagnosed = true;
10670 break;
10671 }
10672
10673 const auto *FirstTemplateArgs =
10674 FirstMethod->getTemplateSpecializationArgs();
10675 const auto *SecondTemplateArgs =
10676 SecondMethod->getTemplateSpecializationArgs();
10677
10678 if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10679 (!FirstTemplateArgs && SecondTemplateArgs)) {
10680 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10681 FirstMethod->getSourceRange(),
10682 MethodNoTemplateArguments)
10683 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10684 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10685 SecondMethod->getSourceRange(),
10686 MethodNoTemplateArguments)
10687 << SecondMethodType << SecondName
10688 << (SecondTemplateArgs != nullptr);
10689
10690 Diagnosed = true;
10691 break;
10692 }
10693
10694 if (FirstTemplateArgs && SecondTemplateArgs) {
10695 // Remove pack expansions from argument list.
10696 auto ExpandTemplateArgumentList =
10697 [](const TemplateArgumentList *TAL) {
10698 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10699 for (const TemplateArgument &TA : TAL->asArray()) {
10700 if (TA.getKind() != TemplateArgument::Pack) {
10701 ExpandedList.push_back(&TA);
10702 continue;
10703 }
10704 for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10705 ExpandedList.push_back(&PackTA);
10706 }
10707 }
10708 return ExpandedList;
10709 };
10710 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10711 ExpandTemplateArgumentList(FirstTemplateArgs);
10712 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10713 ExpandTemplateArgumentList(SecondTemplateArgs);
10714
10715 if (FirstExpandedList.size() != SecondExpandedList.size()) {
10716 ODRDiagDeclError(FirstRecord, FirstModule,
10717 FirstMethod->getLocation(),
10718 FirstMethod->getSourceRange(),
10719 MethodDifferentNumberTemplateArguments)
10720 << FirstMethodType << FirstName
10721 << (unsigned)FirstExpandedList.size();
10722 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10723 SecondMethod->getSourceRange(),
10724 MethodDifferentNumberTemplateArguments)
10725 << SecondMethodType << SecondName
10726 << (unsigned)SecondExpandedList.size();
10727
10728 Diagnosed = true;
10729 break;
10730 }
10731
10732 bool TemplateArgumentMismatch = false;
10733 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10734 const TemplateArgument &FirstTA = *FirstExpandedList[i],
10735 &SecondTA = *SecondExpandedList[i];
10736 if (ComputeTemplateArgumentODRHash(FirstTA) ==
10737 ComputeTemplateArgumentODRHash(SecondTA)) {
10738 continue;
10739 }
10740
10741 ODRDiagDeclError(
10742 FirstRecord, FirstModule, FirstMethod->getLocation(),
10743 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10744 << FirstMethodType << FirstName << FirstTA << i + 1;
10745 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10746 SecondMethod->getSourceRange(),
10747 MethodDifferentTemplateArgument)
10748 << SecondMethodType << SecondName << SecondTA << i + 1;
10749
10750 TemplateArgumentMismatch = true;
10751 break;
10752 }
10753
10754 if (TemplateArgumentMismatch) {
10755 Diagnosed = true;
10756 break;
10757 }
10758 }
10759
10760 // Compute the hash of the method as if it has no body.
10761 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10762 Hash.clear();
10763 Hash.AddFunctionDecl(D, true /*SkipBody*/);
10764 return Hash.CalculateHash();
10765 };
10766
10767 // Compare the hash generated to the hash stored. A difference means
10768 // that a body was present in the original source. Due to merging,
10769 // the stardard way of detecting a body will not work.
10770 const bool HasFirstBody =
10771 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10772 const bool HasSecondBody =
10773 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10774
10775 if (HasFirstBody != HasSecondBody) {
10776 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10777 FirstMethod->getSourceRange(), MethodSingleBody)
10778 << FirstMethodType << FirstName << HasFirstBody;
10779 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10780 SecondMethod->getSourceRange(), MethodSingleBody)
10781 << SecondMethodType << SecondName << HasSecondBody;
10782 Diagnosed = true;
10783 break;
10784 }
10785
10786 if (HasFirstBody && HasSecondBody) {
10787 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10788 FirstMethod->getSourceRange(), MethodDifferentBody)
10789 << FirstMethodType << FirstName;
10790 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10791 SecondMethod->getSourceRange(), MethodDifferentBody)
10792 << SecondMethodType << SecondName;
10793 Diagnosed = true;
10794 break;
10795 }
10796
10797 break;
10798 }
10799 case TypeAlias:
10800 case TypeDef: {
10801 Diagnosed = ODRDiagTypeDefOrAlias(
10802 FirstRecord, FirstModule, SecondModule,
10803 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10804 FirstDiffType == TypeAlias);
10805 break;
10806 }
10807 case Var: {
10808 Diagnosed =
10809 ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10810 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10811 break;
10812 }
10813 case Friend: {
10814 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10815 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10816
10817 NamedDecl *FirstND = FirstFriend->getFriendDecl();
10818 NamedDecl *SecondND = SecondFriend->getFriendDecl();
10819
10820 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10821 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10822
10823 if (FirstND && SecondND) {
10824 ODRDiagDeclError(FirstRecord, FirstModule,
10825 FirstFriend->getFriendLoc(),
10826 FirstFriend->getSourceRange(), FriendFunction)
10827 << FirstND;
10828 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10829 SecondFriend->getSourceRange(), FriendFunction)
10830 << SecondND;
10831
10832 Diagnosed = true;
10833 break;
10834 }
10835
10836 if (FirstTSI && SecondTSI) {
10837 QualType FirstFriendType = FirstTSI->getType();
10838 QualType SecondFriendType = SecondTSI->getType();
10839 assert(ComputeQualTypeODRHash(FirstFriendType) !=
10840 ComputeQualTypeODRHash(SecondFriendType));
10841 ODRDiagDeclError(FirstRecord, FirstModule,
10842 FirstFriend->getFriendLoc(),
10843 FirstFriend->getSourceRange(), FriendType)
10844 << FirstFriendType;
10845 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10846 SecondFriend->getSourceRange(), FriendType)
10847 << SecondFriendType;
10848 Diagnosed = true;
10849 break;
10850 }
10851
10852 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10853 FirstFriend->getSourceRange(), FriendTypeFunction)
10854 << (FirstTSI == nullptr);
10855 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10856 SecondFriend->getSourceRange(), FriendTypeFunction)
10857 << (SecondTSI == nullptr);
10858
10859 Diagnosed = true;
10860 break;
10861 }
10862 case FunctionTemplate: {
10863 FunctionTemplateDecl *FirstTemplate =
10864 cast<FunctionTemplateDecl>(FirstDecl);
10865 FunctionTemplateDecl *SecondTemplate =
10866 cast<FunctionTemplateDecl>(SecondDecl);
10867
10868 TemplateParameterList *FirstTPL =
10869 FirstTemplate->getTemplateParameters();
10870 TemplateParameterList *SecondTPL =
10871 SecondTemplate->getTemplateParameters();
10872
10873 if (FirstTPL->size() != SecondTPL->size()) {
10874 ODRDiagDeclError(FirstRecord, FirstModule,
10875 FirstTemplate->getLocation(),
10876 FirstTemplate->getSourceRange(),
10877 FunctionTemplateDifferentNumberParameters)
10878 << FirstTemplate << FirstTPL->size();
10879 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10880 SecondTemplate->getSourceRange(),
10881 FunctionTemplateDifferentNumberParameters)
10882 << SecondTemplate << SecondTPL->size();
10883
10884 Diagnosed = true;
10885 break;
10886 }
10887
10888 bool ParameterMismatch = false;
10889 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10890 NamedDecl *FirstParam = FirstTPL->getParam(i);
10891 NamedDecl *SecondParam = SecondTPL->getParam(i);
10892
10893 if (FirstParam->getKind() != SecondParam->getKind()) {
10894 enum {
10895 TemplateTypeParameter,
10896 NonTypeTemplateParameter,
10897 TemplateTemplateParameter,
10898 };
10899 auto GetParamType = [](NamedDecl *D) {
10900 switch (D->getKind()) {
10901 default:
10902 llvm_unreachable("Unexpected template parameter type");
10903 case Decl::TemplateTypeParm:
10904 return TemplateTypeParameter;
10905 case Decl::NonTypeTemplateParm:
10906 return NonTypeTemplateParameter;
10907 case Decl::TemplateTemplateParm:
10908 return TemplateTemplateParameter;
10909 }
10910 };
10911
10912 ODRDiagDeclError(FirstRecord, FirstModule,
10913 FirstTemplate->getLocation(),
10914 FirstTemplate->getSourceRange(),
10915 FunctionTemplateParameterDifferentKind)
10916 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10917 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10918 SecondTemplate->getSourceRange(),
10919 FunctionTemplateParameterDifferentKind)
10920 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10921
10922 ParameterMismatch = true;
10923 break;
10924 }
10925
10926 if (FirstParam->getName() != SecondParam->getName()) {
10927 ODRDiagDeclError(
10928 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10929 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10930 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10931 << FirstParam;
10932 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10933 SecondTemplate->getSourceRange(),
10934 FunctionTemplateParameterName)
10935 << SecondTemplate << (i + 1)
10936 << (bool)SecondParam->getIdentifier() << SecondParam;
10937 ParameterMismatch = true;
10938 break;
10939 }
10940
10941 if (isa<TemplateTypeParmDecl>(FirstParam) &&
10942 isa<TemplateTypeParmDecl>(SecondParam)) {
10943 TemplateTypeParmDecl *FirstTTPD =
10944 cast<TemplateTypeParmDecl>(FirstParam);
10945 TemplateTypeParmDecl *SecondTTPD =
10946 cast<TemplateTypeParmDecl>(SecondParam);
10947 bool HasFirstDefaultArgument =
10948 FirstTTPD->hasDefaultArgument() &&
10949 !FirstTTPD->defaultArgumentWasInherited();
10950 bool HasSecondDefaultArgument =
10951 SecondTTPD->hasDefaultArgument() &&
10952 !SecondTTPD->defaultArgumentWasInherited();
10953 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10954 ODRDiagDeclError(FirstRecord, FirstModule,
10955 FirstTemplate->getLocation(),
10956 FirstTemplate->getSourceRange(),
10957 FunctionTemplateParameterSingleDefaultArgument)
10958 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10959 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10960 SecondTemplate->getSourceRange(),
10961 FunctionTemplateParameterSingleDefaultArgument)
10962 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10963 ParameterMismatch = true;
10964 break;
10965 }
10966
10967 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10968 QualType FirstType = FirstTTPD->getDefaultArgument();
10969 QualType SecondType = SecondTTPD->getDefaultArgument();
10970 if (ComputeQualTypeODRHash(FirstType) !=
10971 ComputeQualTypeODRHash(SecondType)) {
10972 ODRDiagDeclError(
10973 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10974 FirstTemplate->getSourceRange(),
10975 FunctionTemplateParameterDifferentDefaultArgument)
10976 << FirstTemplate << (i + 1) << FirstType;
10977 ODRDiagDeclNote(
10978 SecondModule, SecondTemplate->getLocation(),
10979 SecondTemplate->getSourceRange(),
10980 FunctionTemplateParameterDifferentDefaultArgument)
10981 << SecondTemplate << (i + 1) << SecondType;
10982 ParameterMismatch = true;
10983 break;
10984 }
10985 }
10986
10987 if (FirstTTPD->isParameterPack() !=
10988 SecondTTPD->isParameterPack()) {
10989 ODRDiagDeclError(FirstRecord, FirstModule,
10990 FirstTemplate->getLocation(),
10991 FirstTemplate->getSourceRange(),
10992 FunctionTemplatePackParameter)
10993 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10994 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10995 SecondTemplate->getSourceRange(),
10996 FunctionTemplatePackParameter)
10997 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10998 ParameterMismatch = true;
10999 break;
11000 }
11001 }
11002
11003 if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11004 isa<TemplateTemplateParmDecl>(SecondParam)) {
11005 TemplateTemplateParmDecl *FirstTTPD =
11006 cast<TemplateTemplateParmDecl>(FirstParam);
11007 TemplateTemplateParmDecl *SecondTTPD =
11008 cast<TemplateTemplateParmDecl>(SecondParam);
11009
11010 TemplateParameterList *FirstTPL =
11011 FirstTTPD->getTemplateParameters();
11012 TemplateParameterList *SecondTPL =
11013 SecondTTPD->getTemplateParameters();
11014
11015 if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11016 ComputeTemplateParameterListODRHash(SecondTPL)) {
11017 ODRDiagDeclError(FirstRecord, FirstModule,
11018 FirstTemplate->getLocation(),
11019 FirstTemplate->getSourceRange(),
11020 FunctionTemplateParameterDifferentType)
11021 << FirstTemplate << (i + 1);
11022 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11023 SecondTemplate->getSourceRange(),
11024 FunctionTemplateParameterDifferentType)
11025 << SecondTemplate << (i + 1);
11026 ParameterMismatch = true;
11027 break;
11028 }
11029
11030 bool HasFirstDefaultArgument =
11031 FirstTTPD->hasDefaultArgument() &&
11032 !FirstTTPD->defaultArgumentWasInherited();
11033 bool HasSecondDefaultArgument =
11034 SecondTTPD->hasDefaultArgument() &&
11035 !SecondTTPD->defaultArgumentWasInherited();
11036 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11037 ODRDiagDeclError(FirstRecord, FirstModule,
11038 FirstTemplate->getLocation(),
11039 FirstTemplate->getSourceRange(),
11040 FunctionTemplateParameterSingleDefaultArgument)
11041 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11042 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11043 SecondTemplate->getSourceRange(),
11044 FunctionTemplateParameterSingleDefaultArgument)
11045 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11046 ParameterMismatch = true;
11047 break;
11048 }
11049
11050 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11051 TemplateArgument FirstTA =
11052 FirstTTPD->getDefaultArgument().getArgument();
11053 TemplateArgument SecondTA =
11054 SecondTTPD->getDefaultArgument().getArgument();
11055 if (ComputeTemplateArgumentODRHash(FirstTA) !=
11056 ComputeTemplateArgumentODRHash(SecondTA)) {
11057 ODRDiagDeclError(
11058 FirstRecord, FirstModule, FirstTemplate->getLocation(),
11059 FirstTemplate->getSourceRange(),
11060 FunctionTemplateParameterDifferentDefaultArgument)
11061 << FirstTemplate << (i + 1) << FirstTA;
11062 ODRDiagDeclNote(
11063 SecondModule, SecondTemplate->getLocation(),
11064 SecondTemplate->getSourceRange(),
11065 FunctionTemplateParameterDifferentDefaultArgument)
11066 << SecondTemplate << (i + 1) << SecondTA;
11067 ParameterMismatch = true;
11068 break;
11069 }
11070 }
11071
11072 if (FirstTTPD->isParameterPack() !=
11073 SecondTTPD->isParameterPack()) {
11074 ODRDiagDeclError(FirstRecord, FirstModule,
11075 FirstTemplate->getLocation(),
11076 FirstTemplate->getSourceRange(),
11077 FunctionTemplatePackParameter)
11078 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11079 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11080 SecondTemplate->getSourceRange(),
11081 FunctionTemplatePackParameter)
11082 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11083 ParameterMismatch = true;
11084 break;
11085 }
11086 }
11087
11088 if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11089 isa<NonTypeTemplateParmDecl>(SecondParam)) {
11090 NonTypeTemplateParmDecl *FirstNTTPD =
11091 cast<NonTypeTemplateParmDecl>(FirstParam);
11092 NonTypeTemplateParmDecl *SecondNTTPD =
11093 cast<NonTypeTemplateParmDecl>(SecondParam);
11094
11095 QualType FirstType = FirstNTTPD->getType();
11096 QualType SecondType = SecondNTTPD->getType();
11097 if (ComputeQualTypeODRHash(FirstType) !=
11098 ComputeQualTypeODRHash(SecondType)) {
11099 ODRDiagDeclError(FirstRecord, FirstModule,
11100 FirstTemplate->getLocation(),
11101 FirstTemplate->getSourceRange(),
11102 FunctionTemplateParameterDifferentType)
11103 << FirstTemplate << (i + 1);
11104 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11105 SecondTemplate->getSourceRange(),
11106 FunctionTemplateParameterDifferentType)
11107 << SecondTemplate << (i + 1);
11108 ParameterMismatch = true;
11109 break;
11110 }
11111
11112 bool HasFirstDefaultArgument =
11113 FirstNTTPD->hasDefaultArgument() &&
11114 !FirstNTTPD->defaultArgumentWasInherited();
11115 bool HasSecondDefaultArgument =
11116 SecondNTTPD->hasDefaultArgument() &&
11117 !SecondNTTPD->defaultArgumentWasInherited();
11118 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11119 ODRDiagDeclError(FirstRecord, FirstModule,
11120 FirstTemplate->getLocation(),
11121 FirstTemplate->getSourceRange(),
11122 FunctionTemplateParameterSingleDefaultArgument)
11123 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11124 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11125 SecondTemplate->getSourceRange(),
11126 FunctionTemplateParameterSingleDefaultArgument)
11127 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11128 ParameterMismatch = true;
11129 break;
11130 }
11131
11132 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11133 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11134 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11135 if (ComputeODRHash(FirstDefaultArgument) !=
11136 ComputeODRHash(SecondDefaultArgument)) {
11137 ODRDiagDeclError(
11138 FirstRecord, FirstModule, FirstTemplate->getLocation(),
11139 FirstTemplate->getSourceRange(),
11140 FunctionTemplateParameterDifferentDefaultArgument)
11141 << FirstTemplate << (i + 1) << FirstDefaultArgument;
11142 ODRDiagDeclNote(
11143 SecondModule, SecondTemplate->getLocation(),
11144 SecondTemplate->getSourceRange(),
11145 FunctionTemplateParameterDifferentDefaultArgument)
11146 << SecondTemplate << (i + 1) << SecondDefaultArgument;
11147 ParameterMismatch = true;
11148 break;
11149 }
11150 }
11151
11152 if (FirstNTTPD->isParameterPack() !=
11153 SecondNTTPD->isParameterPack()) {
11154 ODRDiagDeclError(FirstRecord, FirstModule,
11155 FirstTemplate->getLocation(),
11156 FirstTemplate->getSourceRange(),
11157 FunctionTemplatePackParameter)
11158 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11159 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11160 SecondTemplate->getSourceRange(),
11161 FunctionTemplatePackParameter)
11162 << SecondTemplate << (i + 1)
11163 << SecondNTTPD->isParameterPack();
11164 ParameterMismatch = true;
11165 break;
11166 }
11167 }
11168 }
11169
11170 if (ParameterMismatch) {
11171 Diagnosed = true;
11172 break;
11173 }
11174
11175 break;
11176 }
11177 }
11178
11179 if (Diagnosed)
11180 continue;
11181
11182 Diag(FirstDecl->getLocation(),
11183 diag::err_module_odr_violation_mismatch_decl_unknown)
11184 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11185 << FirstDecl->getSourceRange();
11186 Diag(SecondDecl->getLocation(),
11187 diag::note_module_odr_violation_mismatch_decl_unknown)
11188 << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11189 Diagnosed = true;
11190 }
11191
11192 if (!Diagnosed) {
11193 // All definitions are updates to the same declaration. This happens if a
11194 // module instantiates the declaration of a class template specialization
11195 // and two or more other modules instantiate its definition.
11196 //
11197 // FIXME: Indicate which modules had instantiations of this definition.
11198 // FIXME: How can this even happen?
11199 Diag(Merge.first->getLocation(),
11200 diag::err_module_odr_violation_different_instantiations)
11201 << Merge.first;
11202 }
11203 }
11204
11205 // Issue ODR failures diagnostics for functions.
11206 for (auto &Merge : FunctionOdrMergeFailures) {
11207 enum ODRFunctionDifference {
11208 ReturnType,
11209 ParameterName,
11210 ParameterType,
11211 ParameterSingleDefaultArgument,
11212 ParameterDifferentDefaultArgument,
11213 FunctionBody,
11214 };
11215
11216 FunctionDecl *FirstFunction = Merge.first;
11217 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11218
11219 bool Diagnosed = false;
11220 for (auto &SecondFunction : Merge.second) {
11221
11222 if (FirstFunction == SecondFunction)
11223 continue;
11224
11225 std::string SecondModule =
11226 getOwningModuleNameForDiagnostic(SecondFunction);
11227
11228 auto ODRDiagError = [FirstFunction, &FirstModule,
11229 this](SourceLocation Loc, SourceRange Range,
11230 ODRFunctionDifference DiffType) {
11231 return Diag(Loc, diag::err_module_odr_violation_function)
11232 << FirstFunction << FirstModule.empty() << FirstModule << Range
11233 << DiffType;
11234 };
11235 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11236 SourceRange Range,
11237 ODRFunctionDifference DiffType) {
11238 return Diag(Loc, diag::note_module_odr_violation_function)
11239 << SecondModule << Range << DiffType;
11240 };
11241
11242 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11243 ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11244 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11245 FirstFunction->getReturnTypeSourceRange(), ReturnType)
11246 << FirstFunction->getReturnType();
11247 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11248 SecondFunction->getReturnTypeSourceRange(), ReturnType)
11249 << SecondFunction->getReturnType();
11250 Diagnosed = true;
11251 break;
11252 }
11253
11254 assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11255 "Merged functions with different number of parameters");
11256
11257 auto ParamSize = FirstFunction->param_size();
11258 bool ParameterMismatch = false;
11259 for (unsigned I = 0; I < ParamSize; ++I) {
11260 auto *FirstParam = FirstFunction->getParamDecl(I);
11261 auto *SecondParam = SecondFunction->getParamDecl(I);
11262
11263 assert(getContext().hasSameType(FirstParam->getType(),
11264 SecondParam->getType()) &&
11265 "Merged function has different parameter types.");
11266
11267 if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11268 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11269 ParameterName)
11270 << I + 1 << FirstParam->getDeclName();
11271 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11272 ParameterName)
11273 << I + 1 << SecondParam->getDeclName();
11274 ParameterMismatch = true;
11275 break;
11276 };
11277
11278 QualType FirstParamType = FirstParam->getType();
11279 QualType SecondParamType = SecondParam->getType();
11280 if (FirstParamType != SecondParamType &&
11281 ComputeQualTypeODRHash(FirstParamType) !=
11282 ComputeQualTypeODRHash(SecondParamType)) {
11283 if (const DecayedType *ParamDecayedType =
11284 FirstParamType->getAs<DecayedType>()) {
11285 ODRDiagError(FirstParam->getLocation(),
11286 FirstParam->getSourceRange(), ParameterType)
11287 << (I + 1) << FirstParamType << true
11288 << ParamDecayedType->getOriginalType();
11289 } else {
11290 ODRDiagError(FirstParam->getLocation(),
11291 FirstParam->getSourceRange(), ParameterType)
11292 << (I + 1) << FirstParamType << false;
11293 }
11294
11295 if (const DecayedType *ParamDecayedType =
11296 SecondParamType->getAs<DecayedType>()) {
11297 ODRDiagNote(SecondParam->getLocation(),
11298 SecondParam->getSourceRange(), ParameterType)
11299 << (I + 1) << SecondParamType << true
11300 << ParamDecayedType->getOriginalType();
11301 } else {
11302 ODRDiagNote(SecondParam->getLocation(),
11303 SecondParam->getSourceRange(), ParameterType)
11304 << (I + 1) << SecondParamType << false;
11305 }
11306 ParameterMismatch = true;
11307 break;
11308 }
11309
11310 const Expr *FirstInit = FirstParam->getInit();
11311 const Expr *SecondInit = SecondParam->getInit();
11312 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11313 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11314 ParameterSingleDefaultArgument)
11315 << (I + 1) << (FirstInit == nullptr)
11316 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11317 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11318 ParameterSingleDefaultArgument)
11319 << (I + 1) << (SecondInit == nullptr)
11320 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11321 ParameterMismatch = true;
11322 break;
11323 }
11324
11325 if (FirstInit && SecondInit &&
11326 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11327 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11328 ParameterDifferentDefaultArgument)
11329 << (I + 1) << FirstInit->getSourceRange();
11330 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11331 ParameterDifferentDefaultArgument)
11332 << (I + 1) << SecondInit->getSourceRange();
11333 ParameterMismatch = true;
11334 break;
11335 }
11336
11337 assert(ComputeSubDeclODRHash(FirstParam) ==
11338 ComputeSubDeclODRHash(SecondParam) &&
11339 "Undiagnosed parameter difference.");
11340 }
11341
11342 if (ParameterMismatch) {
11343 Diagnosed = true;
11344 break;
11345 }
11346
11347 // If no error has been generated before now, assume the problem is in
11348 // the body and generate a message.
11349 ODRDiagError(FirstFunction->getLocation(),
11350 FirstFunction->getSourceRange(), FunctionBody);
11351 ODRDiagNote(SecondFunction->getLocation(),
11352 SecondFunction->getSourceRange(), FunctionBody);
11353 Diagnosed = true;
11354 break;
11355 }
11356 (void)Diagnosed;
11357 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11358 }
11359
11360 // Issue ODR failures diagnostics for enums.
11361 for (auto &Merge : EnumOdrMergeFailures) {
11362 enum ODREnumDifference {
11363 SingleScopedEnum,
11364 EnumTagKeywordMismatch,
11365 SingleSpecifiedType,
11366 DifferentSpecifiedTypes,
11367 DifferentNumberEnumConstants,
11368 EnumConstantName,
11369 EnumConstantSingleInitilizer,
11370 EnumConstantDifferentInitilizer,
11371 };
11372
11373 // If we've already pointed out a specific problem with this enum, don't
11374 // bother issuing a general "something's different" diagnostic.
11375 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11376 continue;
11377
11378 EnumDecl *FirstEnum = Merge.first;
11379 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11380
11381 using DeclHashes =
11382 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11383 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11384 DeclHashes &Hashes, EnumDecl *Enum) {
11385 for (auto *D : Enum->decls()) {
11386 // Due to decl merging, the first EnumDecl is the parent of
11387 // Decls in both records.
11388 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11389 continue;
11390 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11391 Hashes.emplace_back(cast<EnumConstantDecl>(D),
11392 ComputeSubDeclODRHash(D));
11393 }
11394 };
11395 DeclHashes FirstHashes;
11396 PopulateHashes(FirstHashes, FirstEnum);
11397 bool Diagnosed = false;
11398 for (auto &SecondEnum : Merge.second) {
11399
11400 if (FirstEnum == SecondEnum)
11401 continue;
11402
11403 std::string SecondModule =
11404 getOwningModuleNameForDiagnostic(SecondEnum);
11405
11406 auto ODRDiagError = [FirstEnum, &FirstModule,
11407 this](SourceLocation Loc, SourceRange Range,
11408 ODREnumDifference DiffType) {
11409 return Diag(Loc, diag::err_module_odr_violation_enum)
11410 << FirstEnum << FirstModule.empty() << FirstModule << Range
11411 << DiffType;
11412 };
11413 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11414 SourceRange Range,
11415 ODREnumDifference DiffType) {
11416 return Diag(Loc, diag::note_module_odr_violation_enum)
11417 << SecondModule << Range << DiffType;
11418 };
11419
11420 if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11421 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11422 SingleScopedEnum)
11423 << FirstEnum->isScoped();
11424 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11425 SingleScopedEnum)
11426 << SecondEnum->isScoped();
11427 Diagnosed = true;
11428 continue;
11429 }
11430
11431 if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11432 if (FirstEnum->isScopedUsingClassTag() !=
11433 SecondEnum->isScopedUsingClassTag()) {
11434 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11435 EnumTagKeywordMismatch)
11436 << FirstEnum->isScopedUsingClassTag();
11437 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11438 EnumTagKeywordMismatch)
11439 << SecondEnum->isScopedUsingClassTag();
11440 Diagnosed = true;
11441 continue;
11442 }
11443 }
11444
11445 QualType FirstUnderlyingType =
11446 FirstEnum->getIntegerTypeSourceInfo()
11447 ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11448 : QualType();
11449 QualType SecondUnderlyingType =
11450 SecondEnum->getIntegerTypeSourceInfo()
11451 ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11452 : QualType();
11453 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11454 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11455 SingleSpecifiedType)
11456 << !FirstUnderlyingType.isNull();
11457 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11458 SingleSpecifiedType)
11459 << !SecondUnderlyingType.isNull();
11460 Diagnosed = true;
11461 continue;
11462 }
11463
11464 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11465 if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11466 ComputeQualTypeODRHash(SecondUnderlyingType)) {
11467 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11468 DifferentSpecifiedTypes)
11469 << FirstUnderlyingType;
11470 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11471 DifferentSpecifiedTypes)
11472 << SecondUnderlyingType;
11473 Diagnosed = true;
11474 continue;
11475 }
11476 }
11477
11478 DeclHashes SecondHashes;
11479 PopulateHashes(SecondHashes, SecondEnum);
11480
11481 if (FirstHashes.size() != SecondHashes.size()) {
11482 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11483 DifferentNumberEnumConstants)
11484 << (int)FirstHashes.size();
11485 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11486 DifferentNumberEnumConstants)
11487 << (int)SecondHashes.size();
11488 Diagnosed = true;
11489 continue;
11490 }
11491
11492 for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11493 if (FirstHashes[I].second == SecondHashes[I].second)
11494 continue;
11495 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11496 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11497
11498 if (FirstEnumConstant->getDeclName() !=
11499 SecondEnumConstant->getDeclName()) {
11500
11501 ODRDiagError(FirstEnumConstant->getLocation(),
11502 FirstEnumConstant->getSourceRange(), EnumConstantName)
11503 << I + 1 << FirstEnumConstant;
11504 ODRDiagNote(SecondEnumConstant->getLocation(),
11505 SecondEnumConstant->getSourceRange(), EnumConstantName)
11506 << I + 1 << SecondEnumConstant;
11507 Diagnosed = true;
11508 break;
11509 }
11510
11511 const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11512 const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11513 if (!FirstInit && !SecondInit)
11514 continue;
11515
11516 if (!FirstInit || !SecondInit) {
11517 ODRDiagError(FirstEnumConstant->getLocation(),
11518 FirstEnumConstant->getSourceRange(),
11519 EnumConstantSingleInitilizer)
11520 << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11521 ODRDiagNote(SecondEnumConstant->getLocation(),
11522 SecondEnumConstant->getSourceRange(),
11523 EnumConstantSingleInitilizer)
11524 << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11525 Diagnosed = true;
11526 break;
11527 }
11528
11529 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11530 ODRDiagError(FirstEnumConstant->getLocation(),
11531 FirstEnumConstant->getSourceRange(),
11532 EnumConstantDifferentInitilizer)
11533 << I + 1 << FirstEnumConstant;
11534 ODRDiagNote(SecondEnumConstant->getLocation(),
11535 SecondEnumConstant->getSourceRange(),
11536 EnumConstantDifferentInitilizer)
11537 << I + 1 << SecondEnumConstant;
11538 Diagnosed = true;
11539 break;
11540 }
11541 }
11542 }
11543
11544 (void)Diagnosed;
11545 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11546 }
11547}
11548
11549void ASTReader::StartedDeserializing() {
11550 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11551 ReadTimer->startTimer();
11552}
11553
11554void ASTReader::FinishedDeserializing() {
11555 assert(NumCurrentElementsDeserializing &&
11556 "FinishedDeserializing not paired with StartedDeserializing");
11557 if (NumCurrentElementsDeserializing == 1) {
11558 // We decrease NumCurrentElementsDeserializing only after pending actions
11559 // are finished, to avoid recursively re-calling finishPendingActions().
11560 finishPendingActions();
11561 }
11562 --NumCurrentElementsDeserializing;
11563
11564 if (NumCurrentElementsDeserializing == 0) {
11565 // Propagate exception specification and deduced type updates along
11566 // redeclaration chains.
11567 //
11568 // We do this now rather than in finishPendingActions because we want to
11569 // be able to walk the complete redeclaration chains of the updated decls.
11570 while (!PendingExceptionSpecUpdates.empty() ||
11571 !PendingDeducedTypeUpdates.empty()) {
11572 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11573 PendingExceptionSpecUpdates.clear();
11574 for (auto Update : ESUpdates) {
11575 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11576 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11577 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11578 if (auto *Listener = getContext().getASTMutationListener())
11579 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11580 for (auto *Redecl : Update.second->redecls())
11581 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11582 }
11583
11584 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11585 PendingDeducedTypeUpdates.clear();
11586 for (auto Update : DTUpdates) {
11587 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11588 // FIXME: If the return type is already deduced, check that it matches.
11589 getContext().adjustDeducedFunctionResultType(Update.first,
11590 Update.second);
11591 }
11592 }
11593
11594 if (ReadTimer)
11595 ReadTimer->stopTimer();
11596
11597 diagnoseOdrViolations();
11598
11599 // We are not in recursive loading, so it's safe to pass the "interesting"
11600 // decls to the consumer.
11601 if (Consumer)
11602 PassInterestingDeclsToConsumer();
11603 }
11604}
11605
11606void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11607 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11608 // Remove any fake results before adding any real ones.
11609 auto It = PendingFakeLookupResults.find(II);
11610 if (It != PendingFakeLookupResults.end()) {
11611 for (auto *ND : It->second)
11612 SemaObj->IdResolver.RemoveDecl(ND);
11613 // FIXME: this works around module+PCH performance issue.
11614 // Rather than erase the result from the map, which is O(n), just clear
11615 // the vector of NamedDecls.
11616 It->second.clear();
11617 }
11618 }
11619
11620 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11621 SemaObj->TUScope->AddDecl(D);
11622 } else if (SemaObj->TUScope) {
11623 // Adding the decl to IdResolver may have failed because it was already in
11624 // (even though it was not added in scope). If it is already in, make sure
11625 // it gets in the scope as well.
11626 if (std::find(SemaObj->IdResolver.begin(Name),
11627 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11628 SemaObj->TUScope->AddDecl(D);
11629 }
11630}
11631
11632ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11633 ASTContext *Context,
11634 const PCHContainerReader &PCHContainerRdr,
11635 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11636 StringRef isysroot,
11637 DisableValidationForModuleKind DisableValidationKind,
11638 bool AllowASTWithCompilerErrors,
11639 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11640 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11641 std::unique_ptr<llvm::Timer> ReadTimer)
11642 : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH)
11643 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11644 : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11645 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11646 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11647 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11648 PCHContainerRdr, PP.getHeaderSearchInfo()),
11649 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11650 DisableValidationKind(DisableValidationKind),
11651 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11652 AllowConfigurationMismatch(AllowConfigurationMismatch),
11653 ValidateSystemInputs(ValidateSystemInputs),
11654 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11655 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11656 SourceMgr.setExternalSLocEntrySource(this);
11657
11658 for (const auto &Ext : Extensions) {
11659 auto BlockName = Ext->getExtensionMetadata().BlockName;
11660 auto Known = ModuleFileExtensions.find(BlockName);
11661 if (Known != ModuleFileExtensions.end()) {
11662 Diags.Report(diag::warn_duplicate_module_file_extension)
11663 << BlockName;
11664 continue;
11665 }
11666
11667 ModuleFileExtensions.insert({BlockName, Ext});
11668 }
11669}
11670
11671ASTReader::~ASTReader() {
11672 if (OwnsDeserializationListener)
11673 delete DeserializationListener;
11674}
11675
11676IdentifierResolver &ASTReader::getIdResolver() {
11677 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11678}
11679
11680Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11681 unsigned AbbrevID) {
11682 Idx = 0;
11683 Record.clear();
11684 return Cursor.readRecord(AbbrevID, Record);
11685}
11686//===----------------------------------------------------------------------===//
11687//// OMPClauseReader implementation
11688////===----------------------------------------------------------------------===//
11689
11690// This has to be in namespace clang because it's friended by all
11691// of the OMP clauses.
11692namespace clang {
11693
11694class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11695 ASTRecordReader &Record;
11696 ASTContext &Context;
11697
11698public:
11699 OMPClauseReader(ASTRecordReader &Record)
11700 : Record(Record), Context(Record.getContext()) {}
11701#define GEN_CLANG_CLAUSE_CLASS
11702#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11703#include "llvm/Frontend/OpenMP/OMP.inc"
11704 OMPClause *readClause();
11705 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11706 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11707};
11708
11709} // end namespace clang
11710
11711OMPClause *ASTRecordReader::readOMPClause() {
11712 return OMPClauseReader(*this).readClause();
11713}
11714
11715OMPClause *OMPClauseReader::readClause() {
11716 OMPClause *C = nullptr;
11717 switch (llvm::omp::Clause(Record.readInt())) {
11718 case llvm::omp::OMPC_if:
11719 C = new (Context) OMPIfClause();
11720 break;
11721 case llvm::omp::OMPC_final:
11722 C = new (Context) OMPFinalClause();
11723 break;
11724 case llvm::omp::OMPC_num_threads:
11725 C = new (Context) OMPNumThreadsClause();
11726 break;
11727 case llvm::omp::OMPC_safelen:
11728 C = new (Context) OMPSafelenClause();
11729 break;
11730 case llvm::omp::OMPC_simdlen:
11731 C = new (Context) OMPSimdlenClause();
11732 break;
11733 case llvm::omp::OMPC_allocator:
11734 C = new (Context) OMPAllocatorClause();
11735 break;
11736 case llvm::omp::OMPC_collapse:
11737 C = new (Context) OMPCollapseClause();
11738 break;
11739 case llvm::omp::OMPC_default:
11740 C = new (Context) OMPDefaultClause();
11741 break;
11742 case llvm::omp::OMPC_proc_bind:
11743 C = new (Context) OMPProcBindClause();
11744 break;
11745 case llvm::omp::OMPC_schedule:
11746 C = new (Context) OMPScheduleClause();
11747 break;
11748 case llvm::omp::OMPC_ordered:
11749 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11750 break;
11751 case llvm::omp::OMPC_nowait:
11752 C = new (Context) OMPNowaitClause();
11753 break;
11754 case llvm::omp::OMPC_untied:
11755 C = new (Context) OMPUntiedClause();
11756 break;
11757 case llvm::omp::OMPC_mergeable:
11758 C = new (Context) OMPMergeableClause();
11759 break;
11760 case llvm::omp::OMPC_read:
11761 C = new (Context) OMPReadClause();
11762 break;
11763 case llvm::omp::OMPC_write:
11764 C = new (Context) OMPWriteClause();
11765 break;
11766 case llvm::omp::OMPC_update:
11767 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11768 break;
11769 case llvm::omp::OMPC_capture:
11770 C = new (Context) OMPCaptureClause();
11771 break;
11772 case llvm::omp::OMPC_seq_cst:
11773 C = new (Context) OMPSeqCstClause();
11774 break;
11775 case llvm::omp::OMPC_acq_rel:
11776 C = new (Context) OMPAcqRelClause();
11777 break;
11778 case llvm::omp::OMPC_acquire:
11779 C = new (Context) OMPAcquireClause();
11780 break;
11781 case llvm::omp::OMPC_release:
11782 C = new (Context) OMPReleaseClause();
11783 break;
11784 case llvm::omp::OMPC_relaxed:
11785 C = new (Context) OMPRelaxedClause();
11786 break;
11787 case llvm::omp::OMPC_threads:
11788 C = new (Context) OMPThreadsClause();
11789 break;
11790 case llvm::omp::OMPC_simd:
11791 C = new (Context) OMPSIMDClause();
11792 break;
11793 case llvm::omp::OMPC_nogroup:
11794 C = new (Context) OMPNogroupClause();
11795 break;
11796 case llvm::omp::OMPC_unified_address:
11797 C = new (Context) OMPUnifiedAddressClause();
11798 break;
11799 case llvm::omp::OMPC_unified_shared_memory:
11800 C = new (Context) OMPUnifiedSharedMemoryClause();
11801 break;
11802 case llvm::omp::OMPC_reverse_offload:
11803 C = new (Context) OMPReverseOffloadClause();
11804 break;
11805 case llvm::omp::OMPC_dynamic_allocators:
11806 C = new (Context) OMPDynamicAllocatorsClause();
11807 break;
11808 case llvm::omp::OMPC_atomic_default_mem_order:
11809 C = new (Context) OMPAtomicDefaultMemOrderClause();
11810 break;
11811 case llvm::omp::OMPC_private:
11812 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11813 break;
11814 case llvm::omp::OMPC_firstprivate:
11815 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11816 break;
11817 case llvm::omp::OMPC_lastprivate:
11818 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11819 break;
11820 case llvm::omp::OMPC_shared:
11821 C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11822 break;
11823 case llvm::omp::OMPC_reduction: {
11824 unsigned N = Record.readInt();
11825 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11826 C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11827 break;
11828 }
11829 case llvm::omp::OMPC_task_reduction:
11830 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11831 break;
11832 case llvm::omp::OMPC_in_reduction:
11833 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11834 break;
11835 case llvm::omp::OMPC_linear:
11836 C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11837 break;
11838 case llvm::omp::OMPC_aligned:
11839 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11840 break;
11841 case llvm::omp::OMPC_copyin:
11842 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11843 break;
11844 case llvm::omp::OMPC_copyprivate:
11845 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11846 break;
11847 case llvm::omp::OMPC_flush:
11848 C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11849 break;
11850 case llvm::omp::OMPC_depobj:
11851 C = OMPDepobjClause::CreateEmpty(Context);
11852 break;
11853 case llvm::omp::OMPC_depend: {
11854 unsigned NumVars = Record.readInt();
11855 unsigned NumLoops = Record.readInt();
11856 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11857 break;
11858 }
11859 case llvm::omp::OMPC_device:
11860 C = new (Context) OMPDeviceClause();
11861 break;
11862 case llvm::omp::OMPC_map: {
11863 OMPMappableExprListSizeTy Sizes;
11864 Sizes.NumVars = Record.readInt();
11865 Sizes.NumUniqueDeclarations = Record.readInt();
11866 Sizes.NumComponentLists = Record.readInt();
11867 Sizes.NumComponents = Record.readInt();
11868 C = OMPMapClause::CreateEmpty(Context, Sizes);
11869 break;
11870 }
11871 case llvm::omp::OMPC_num_teams:
11872 C = new (Context) OMPNumTeamsClause();
11873 break;
11874 case llvm::omp::OMPC_thread_limit:
11875 C = new (Context) OMPThreadLimitClause();
11876 break;
11877 case llvm::omp::OMPC_priority:
11878 C = new (Context) OMPPriorityClause();
11879 break;
11880 case llvm::omp::OMPC_grainsize:
11881 C = new (Context) OMPGrainsizeClause();
11882 break;
11883 case llvm::omp::OMPC_num_tasks:
11884 C = new (Context) OMPNumTasksClause();
11885 break;
11886 case llvm::omp::OMPC_hint:
11887 C = new (Context) OMPHintClause();
11888 break;
11889 case llvm::omp::OMPC_dist_schedule:
11890 C = new (Context) OMPDistScheduleClause();
11891 break;
11892 case llvm::omp::OMPC_defaultmap:
11893 C = new (Context) OMPDefaultmapClause();
11894 break;
11895 case llvm::omp::OMPC_to: {
11896 OMPMappableExprListSizeTy Sizes;
11897 Sizes.NumVars = Record.readInt();
11898 Sizes.NumUniqueDeclarations = Record.readInt();
11899 Sizes.NumComponentLists = Record.readInt();
11900 Sizes.NumComponents = Record.readInt();
11901 C = OMPToClause::CreateEmpty(Context, Sizes);
11902 break;
11903 }
11904 case llvm::omp::OMPC_from: {
11905 OMPMappableExprListSizeTy Sizes;
11906 Sizes.NumVars = Record.readInt();
11907 Sizes.NumUniqueDeclarations = Record.readInt();
11908 Sizes.NumComponentLists = Record.readInt();
11909 Sizes.NumComponents = Record.readInt();
11910 C = OMPFromClause::CreateEmpty(Context, Sizes);
11911 break;
11912 }
11913 case llvm::omp::OMPC_use_device_ptr: {
11914 OMPMappableExprListSizeTy Sizes;
11915 Sizes.NumVars = Record.readInt();
11916 Sizes.NumUniqueDeclarations = Record.readInt();
11917 Sizes.NumComponentLists = Record.readInt();
11918 Sizes.NumComponents = Record.readInt();
11919 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11920 break;
11921 }
11922 case llvm::omp::OMPC_use_device_addr: {
11923 OMPMappableExprListSizeTy Sizes;
11924 Sizes.NumVars = Record.readInt();
11925 Sizes.NumUniqueDeclarations = Record.readInt();
11926 Sizes.NumComponentLists = Record.readInt();
11927 Sizes.NumComponents = Record.readInt();
11928 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11929 break;
11930 }
11931 case llvm::omp::OMPC_is_device_ptr: {
11932 OMPMappableExprListSizeTy Sizes;
11933 Sizes.NumVars = Record.readInt();
11934 Sizes.NumUniqueDeclarations = Record.readInt();
11935 Sizes.NumComponentLists = Record.readInt();
11936 Sizes.NumComponents = Record.readInt();
11937 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11938 break;
11939 }
11940 case llvm::omp::OMPC_allocate:
11941 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11942 break;
11943 case llvm::omp::OMPC_nontemporal:
11944 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11945 break;
11946 case llvm::omp::OMPC_inclusive:
11947 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11948 break;
11949 case llvm::omp::OMPC_exclusive:
11950 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11951 break;
11952 case llvm::omp::OMPC_order:
11953 C = new (Context) OMPOrderClause();
11954 break;
11955 case llvm::omp::OMPC_destroy:
11956 C = new (Context) OMPDestroyClause();
11957 break;
11958 case llvm::omp::OMPC_detach:
11959 C = new (Context) OMPDetachClause();
11960 break;
11961 case llvm::omp::OMPC_uses_allocators:
11962 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11963 break;
11964 case llvm::omp::OMPC_affinity:
11965 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11966 break;
11967#define OMP_CLAUSE_NO_CLASS(Enum, Str) \
11968 case llvm::omp::Enum: \
11969 break;
11970#include "llvm/Frontend/OpenMP/OMPKinds.def"
11971 default:
11972 break;
11973 }
11974 assert(C && "Unknown OMPClause type");
11975
11976 Visit(C);
11977 C->setLocStart(Record.readSourceLocation());
11978 C->setLocEnd(Record.readSourceLocation());
11979
11980 return C;
11981}
11982
11983void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11984 C->setPreInitStmt(Record.readSubStmt(),
11985 static_cast<OpenMPDirectiveKind>(Record.readInt()));
11986}
11987
11988void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11989 VisitOMPClauseWithPreInit(C);
11990 C->setPostUpdateExpr(Record.readSubExpr());
11991}
11992
11993void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11994 VisitOMPClauseWithPreInit(C);
11995 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11996 C->setNameModifierLoc(Record.readSourceLocation());
11997 C->setColonLoc(Record.readSourceLocation());
11998 C->setCondition(Record.readSubExpr());
11999 C->setLParenLoc(Record.readSourceLocation());
12000}
12001
12002void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12003 VisitOMPClauseWithPreInit(C);
12004 C->setCondition(Record.readSubExpr());
12005 C->setLParenLoc(Record.readSourceLocation());
12006}
12007
12008void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12009 VisitOMPClauseWithPreInit(C);
12010 C->setNumThreads(Record.readSubExpr());
12011 C->setLParenLoc(Record.readSourceLocation());
12012}
12013
12014void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12015 C->setSafelen(Record.readSubExpr());
12016 C->setLParenLoc(Record.readSourceLocation());
12017}
12018
12019void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12020 C->setSimdlen(Record.readSubExpr());
12021 C->setLParenLoc(Record.readSourceLocation());
12022}
12023
12024void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12025 C->setAllocator(Record.readExpr());
12026 C->setLParenLoc(Record.readSourceLocation());
12027}
12028
12029void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12030 C->setNumForLoops(Record.readSubExpr());
12031 C->setLParenLoc(Record.readSourceLocation());
12032}
12033
12034void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12035 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12036 C->setLParenLoc(Record.readSourceLocation());
12037 C->setDefaultKindKwLoc(Record.readSourceLocation());
12038}
12039
12040void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12041 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12042 C->setLParenLoc(Record.readSourceLocation());
12043 C->setProcBindKindKwLoc(Record.readSourceLocation());
12044}
12045
12046void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12047 VisitOMPClauseWithPreInit(C);
12048 C->setScheduleKind(
12049 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12050 C->setFirstScheduleModifier(
12051 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12052 C->setSecondScheduleModifier(
12053 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12054 C->setChunkSize(Record.readSubExpr());
12055 C->setLParenLoc(Record.readSourceLocation());
12056 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12057 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12058 C->setScheduleKindLoc(Record.readSourceLocation());
12059 C->setCommaLoc(Record.readSourceLocation());
12060}
12061
12062void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12063 C->setNumForLoops(Record.readSubExpr());
12064 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12065 C->setLoopNumIterations(I, Record.readSubExpr());
12066 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12067 C->setLoopCounter(I, Record.readSubExpr());
12068 C->setLParenLoc(Record.readSourceLocation());
12069}
12070
12071void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12072 C->setEventHandler(Record.readSubExpr());
12073 C->setLParenLoc(Record.readSourceLocation());
12074}
12075
12076void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12077
12078void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12079
12080void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12081
12082void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12083
12084void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12085
12086void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12087 if (C->isExtended()) {
12088 C->setLParenLoc(Record.readSourceLocation());
12089 C->setArgumentLoc(Record.readSourceLocation());
12090 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12091 }
12092}
12093
12094void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12095
12096void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12097
12098void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12099
12100void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12101
12102void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12103
12104void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12105
12106void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12107
12108void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12109
12110void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12111
12112void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {}
12113
12114void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12115
12116void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12117 OMPUnifiedSharedMemoryClause *) {}
12118
12119void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12120
12121void
12122OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12123}
12124
12125void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12126 OMPAtomicDefaultMemOrderClause *C) {
12127 C->setAtomicDefaultMemOrderKind(
12128 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12129 C->setLParenLoc(Record.readSourceLocation());
12130 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12131}
12132
12133void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12134 C->setLParenLoc(Record.readSourceLocation());
12135 unsigned NumVars = C->varlist_size();
12136 SmallVector<Expr *, 16> Vars;
12137 Vars.reserve(NumVars);
12138 for (unsigned i = 0; i != NumVars; ++i)
12139 Vars.push_back(Record.readSubExpr());
12140 C->setVarRefs(Vars);
12141 Vars.clear();
12142 for (unsigned i = 0; i != NumVars; ++i)
12143 Vars.push_back(Record.readSubExpr());
12144 C->setPrivateCopies(Vars);
12145}
12146
12147void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12148 VisitOMPClauseWithPreInit(C);
12149 C->setLParenLoc(Record.readSourceLocation());
12150 unsigned NumVars = C->varlist_size();
12151 SmallVector<Expr *, 16> Vars;
12152 Vars.reserve(NumVars);
12153 for (unsigned i = 0; i != NumVars; ++i)
12154 Vars.push_back(Record.readSubExpr());
12155 C->setVarRefs(Vars);
12156 Vars.clear();
12157 for (unsigned i = 0; i != NumVars; ++i)
12158 Vars.push_back(Record.readSubExpr());
12159 C->setPrivateCopies(Vars);
12160 Vars.clear();
12161 for (unsigned i = 0; i != NumVars; ++i)
12162 Vars.push_back(Record.readSubExpr());
12163 C->setInits(Vars);
12164}
12165
12166void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12167 VisitOMPClauseWithPostUpdate(C);
12168 C->setLParenLoc(Record.readSourceLocation());
12169 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12170 C->setKindLoc(Record.readSourceLocation());
12171 C->setColonLoc(Record.readSourceLocation());
12172 unsigned NumVars = C->varlist_size();
12173 SmallVector<Expr *, 16> Vars;
12174 Vars.reserve(NumVars);
12175 for (unsigned i = 0; i != NumVars; ++i)
12176 Vars.push_back(Record.readSubExpr());
12177 C->setVarRefs(Vars);
12178 Vars.clear();
12179 for (unsigned i = 0; i != NumVars; ++i)
12180 Vars.push_back(Record.readSubExpr());
12181 C->setPrivateCopies(Vars);
12182 Vars.clear();
12183 for (unsigned i = 0; i != NumVars; ++i)
12184 Vars.push_back(Record.readSubExpr());
12185 C->setSourceExprs(Vars);
12186 Vars.clear();
12187 for (unsigned i = 0; i != NumVars; ++i)
12188 Vars.push_back(Record.readSubExpr());
12189 C->setDestinationExprs(Vars);
12190 Vars.clear();
12191 for (unsigned i = 0; i != NumVars; ++i)
12192 Vars.push_back(Record.readSubExpr());
12193 C->setAssignmentOps(Vars);
12194}
12195
12196void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12197 C->setLParenLoc(Record.readSourceLocation());
12198 unsigned NumVars = C->varlist_size();
12199 SmallVector<Expr *, 16> Vars;
12200 Vars.reserve(NumVars);
12201 for (unsigned i = 0; i != NumVars; ++i)
12202 Vars.push_back(Record.readSubExpr());
12203 C->setVarRefs(Vars);
12204}
12205
12206void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12207 VisitOMPClauseWithPostUpdate(C);
12208 C->setLParenLoc(Record.readSourceLocation());
12209 C->setModifierLoc(Record.readSourceLocation());
12210 C->setColonLoc(Record.readSourceLocation());
12211 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12212 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12213 C->setQualifierLoc(NNSL);
12214 C->setNameInfo(DNI);
12215
12216 unsigned NumVars = C->varlist_size();
12217 SmallVector<Expr *, 16> Vars;
12218 Vars.reserve(NumVars);
12219 for (unsigned i = 0; i != NumVars; ++i)
12220 Vars.push_back(Record.readSubExpr());
12221 C->setVarRefs(Vars);
12222 Vars.clear();
12223 for (unsigned i = 0; i != NumVars; ++i)
12224 Vars.push_back(Record.readSubExpr());
12225 C->setPrivates(Vars);
12226 Vars.clear();
12227 for (unsigned i = 0; i != NumVars; ++i)
12228 Vars.push_back(Record.readSubExpr());
12229 C->setLHSExprs(Vars);
12230 Vars.clear();
12231 for (unsigned i = 0; i != NumVars; ++i)
12232 Vars.push_back(Record.readSubExpr());
12233 C->setRHSExprs(Vars);
12234 Vars.clear();
12235 for (unsigned i = 0; i != NumVars; ++i)
12236 Vars.push_back(Record.readSubExpr());
12237 C->setReductionOps(Vars);
12238 if (C->getModifier() == OMPC_REDUCTION_inscan) {
12239 Vars.clear();
12240 for (unsigned i = 0; i != NumVars; ++i)
12241 Vars.push_back(Record.readSubExpr());
12242 C->setInscanCopyOps(Vars);
12243 Vars.clear();
12244 for (unsigned i = 0; i != NumVars; ++i)
12245 Vars.push_back(Record.readSubExpr());
12246 C->setInscanCopyArrayTemps(Vars);
12247 Vars.clear();
12248 for (unsigned i = 0; i != NumVars; ++i)
12249 Vars.push_back(Record.readSubExpr());
12250 C->setInscanCopyArrayElems(Vars);
12251 }
12252}
12253
12254void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12255 VisitOMPClauseWithPostUpdate(C);
12256 C->setLParenLoc(Record.readSourceLocation());
12257 C->setColonLoc(Record.readSourceLocation());
12258 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12259 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12260 C->setQualifierLoc(NNSL);
12261 C->setNameInfo(DNI);
12262
12263 unsigned NumVars = C->varlist_size();
12264 SmallVector<Expr *, 16> Vars;
12265 Vars.reserve(NumVars);
12266 for (unsigned I = 0; I != NumVars; ++I)
12267 Vars.push_back(Record.readSubExpr());
12268 C->setVarRefs(Vars);
12269 Vars.clear();
12270 for (unsigned I = 0; I != NumVars; ++I)
12271 Vars.push_back(Record.readSubExpr());
12272 C->setPrivates(Vars);
12273 Vars.clear();
12274 for (unsigned I = 0; I != NumVars; ++I)
12275 Vars.push_back(Record.readSubExpr());
12276 C->setLHSExprs(Vars);
12277 Vars.clear();
12278 for (unsigned I = 0; I != NumVars; ++I)
12279 Vars.push_back(Record.readSubExpr());
12280 C->setRHSExprs(Vars);
12281 Vars.clear();
12282 for (unsigned I = 0; I != NumVars; ++I)
12283 Vars.push_back(Record.readSubExpr());
12284 C->setReductionOps(Vars);
12285}
12286
12287void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12288 VisitOMPClauseWithPostUpdate(C);
12289 C->setLParenLoc(Record.readSourceLocation());
12290 C->setColonLoc(Record.readSourceLocation());
12291 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12292 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12293 C->setQualifierLoc(NNSL);
12294 C->setNameInfo(DNI);
12295
12296 unsigned NumVars = C->varlist_size();
12297 SmallVector<Expr *, 16> Vars;
12298 Vars.reserve(NumVars);
12299 for (unsigned I = 0; I != NumVars; ++I)
12300 Vars.push_back(Record.readSubExpr());
12301 C->setVarRefs(Vars);
12302 Vars.clear();
12303 for (unsigned I = 0; I != NumVars; ++I)
12304 Vars.push_back(Record.readSubExpr());
12305 C->setPrivates(Vars);
12306 Vars.clear();
12307 for (unsigned I = 0; I != NumVars; ++I)
12308 Vars.push_back(Record.readSubExpr());
12309 C->setLHSExprs(Vars);
12310 Vars.clear();
12311 for (unsigned I = 0; I != NumVars; ++I)
12312 Vars.push_back(Record.readSubExpr());
12313 C->setRHSExprs(Vars);
12314 Vars.clear();
12315 for (unsigned I = 0; I != NumVars; ++I)
12316 Vars.push_back(Record.readSubExpr());
12317 C->setReductionOps(Vars);
12318 Vars.clear();
12319 for (unsigned I = 0; I != NumVars; ++I)
12320 Vars.push_back(Record.readSubExpr());
12321 C->setTaskgroupDescriptors(Vars);
12322}
12323
12324void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12325 VisitOMPClauseWithPostUpdate(C);
12326 C->setLParenLoc(Record.readSourceLocation());
12327 C->setColonLoc(Record.readSourceLocation());
12328 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12329 C->setModifierLoc(Record.readSourceLocation());
12330 unsigned NumVars = C->varlist_size();
12331 SmallVector<Expr *, 16> Vars;
12332 Vars.reserve(NumVars);
12333 for (unsigned i = 0; i != NumVars; ++i)
12334 Vars.push_back(Record.readSubExpr());
12335 C->setVarRefs(Vars);
12336 Vars.clear();
12337 for (unsigned i = 0; i != NumVars; ++i)
12338 Vars.push_back(Record.readSubExpr());
12339 C->setPrivates(Vars);
12340 Vars.clear();
12341 for (unsigned i = 0; i != NumVars; ++i)
12342 Vars.push_back(Record.readSubExpr());
12343 C->setInits(Vars);
12344 Vars.clear();
12345 for (unsigned i = 0; i != NumVars; ++i)
12346 Vars.push_back(Record.readSubExpr());
12347 C->setUpdates(Vars);
12348 Vars.clear();
12349 for (unsigned i = 0; i != NumVars; ++i)
12350 Vars.push_back(Record.readSubExpr());
12351 C->setFinals(Vars);
12352 C->setStep(Record.readSubExpr());
12353 C->setCalcStep(Record.readSubExpr());
12354 Vars.clear();
12355 for (unsigned I = 0; I != NumVars + 1; ++I)
12356 Vars.push_back(Record.readSubExpr());
12357 C->setUsedExprs(Vars);
12358}
12359
12360void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12361 C->setLParenLoc(Record.readSourceLocation());
12362 C->setColonLoc(Record.readSourceLocation());
12363 unsigned NumVars = C->varlist_size();
12364 SmallVector<Expr *, 16> Vars;
12365 Vars.reserve(NumVars);
12366 for (unsigned i = 0; i != NumVars; ++i)
12367 Vars.push_back(Record.readSubExpr());
12368 C->setVarRefs(Vars);
12369 C->setAlignment(Record.readSubExpr());
12370}
12371
12372void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12373 C->setLParenLoc(Record.readSourceLocation());
12374 unsigned NumVars = C->varlist_size();
12375 SmallVector<Expr *, 16> Exprs;
12376 Exprs.reserve(NumVars);
12377 for (unsigned i = 0; i != NumVars; ++i)
12378 Exprs.push_back(Record.readSubExpr());
12379 C->setVarRefs(Exprs);
12380 Exprs.clear();
12381 for (unsigned i = 0; i != NumVars; ++i)
12382 Exprs.push_back(Record.readSubExpr());
12383 C->setSourceExprs(Exprs);
12384 Exprs.clear();
12385 for (unsigned i = 0; i != NumVars; ++i)
12386 Exprs.push_back(Record.readSubExpr());
12387 C->setDestinationExprs(Exprs);
12388 Exprs.clear();
12389 for (unsigned i = 0; i != NumVars; ++i)
12390 Exprs.push_back(Record.readSubExpr());
12391 C->setAssignmentOps(Exprs);
12392}
12393
12394void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12395 C->setLParenLoc(Record.readSourceLocation());
12396 unsigned NumVars = C->varlist_size();
12397 SmallVector<Expr *, 16> Exprs;
12398 Exprs.reserve(NumVars);
12399 for (unsigned i = 0; i != NumVars; ++i)
12400 Exprs.push_back(Record.readSubExpr());
12401 C->setVarRefs(Exprs);
12402 Exprs.clear();
12403 for (unsigned i = 0; i != NumVars; ++i)
12404 Exprs.push_back(Record.readSubExpr());
12405 C->setSourceExprs(Exprs);
12406 Exprs.clear();
12407 for (unsigned i = 0; i != NumVars; ++i)
12408 Exprs.push_back(Record.readSubExpr());
12409 C->setDestinationExprs(Exprs);
12410 Exprs.clear();
12411 for (unsigned i = 0; i != NumVars; ++i)
12412 Exprs.push_back(Record.readSubExpr());
12413 C->setAssignmentOps(Exprs);
12414}
12415
12416void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12417 C->setLParenLoc(Record.readSourceLocation());
12418 unsigned NumVars = C->varlist_size();
12419 SmallVector<Expr *, 16> Vars;
12420 Vars.reserve(NumVars);
12421 for (unsigned i = 0; i != NumVars; ++i)
12422 Vars.push_back(Record.readSubExpr());
12423 C->setVarRefs(Vars);
12424}
12425
12426void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12427 C->setDepobj(Record.readSubExpr());
12428 C->setLParenLoc(Record.readSourceLocation());
12429}
12430
12431void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12432 C->setLParenLoc(Record.readSourceLocation());
12433 C->setModifier(Record.readSubExpr());
12434 C->setDependencyKind(
12435 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12436 C->setDependencyLoc(Record.readSourceLocation());
12437 C->setColonLoc(Record.readSourceLocation());
12438 unsigned NumVars = C->varlist_size();
12439 SmallVector<Expr *, 16> Vars;
12440 Vars.reserve(NumVars);
12441 for (unsigned I = 0; I != NumVars; ++I)
12442 Vars.push_back(Record.readSubExpr());
12443 C->setVarRefs(Vars);
12444 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12445 C->setLoopData(I, Record.readSubExpr());
12446}
12447
12448void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12449 VisitOMPClauseWithPreInit(C);
12450 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12451 C->setDevice(Record.readSubExpr());
12452 C->setModifierLoc(Record.readSourceLocation());
12453 C->setLParenLoc(Record.readSourceLocation());
12454}
12455
12456void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12457 C->setLParenLoc(Record.readSourceLocation());
12458 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12459 C->setMapTypeModifier(
12460 I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12461 C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12462 }
12463 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12464 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12465 C->setMapType(
12466 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12467 C->setMapLoc(Record.readSourceLocation());
12468 C->setColonLoc(Record.readSourceLocation());
12469 auto NumVars = C->varlist_size();
12470 auto UniqueDecls = C->getUniqueDeclarationsNum();
12471 auto TotalLists = C->getTotalComponentListNum();
12472 auto TotalComponents = C->getTotalComponentsNum();
12473
12474 SmallVector<Expr *, 16> Vars;
12475 Vars.reserve(NumVars);
12476 for (unsigned i = 0; i != NumVars; ++i)
12477 Vars.push_back(Record.readExpr());
12478 C->setVarRefs(Vars);
12479
12480 SmallVector<Expr *, 16> UDMappers;
12481 UDMappers.reserve(NumVars);
12482 for (unsigned I = 0; I < NumVars; ++I)
12483 UDMappers.push_back(Record.readExpr());
12484 C->setUDMapperRefs(UDMappers);
12485
12486 SmallVector<ValueDecl *, 16> Decls;
12487 Decls.reserve(UniqueDecls);
12488 for (unsigned i = 0; i < UniqueDecls; ++i)
12489 Decls.push_back(Record.readDeclAs<ValueDecl>());
12490 C->setUniqueDecls(Decls);
12491
12492 SmallVector<unsigned, 16> ListsPerDecl;
12493 ListsPerDecl.reserve(UniqueDecls);
12494 for (unsigned i = 0; i < UniqueDecls; ++i)
12495 ListsPerDecl.push_back(Record.readInt());
12496 C->setDeclNumLists(ListsPerDecl);
12497
12498 SmallVector<unsigned, 32> ListSizes;
12499 ListSizes.reserve(TotalLists);
12500 for (unsigned i = 0; i < TotalLists; ++i)
12501 ListSizes.push_back(Record.readInt());
12502 C->setComponentListSizes(ListSizes);
12503
12504 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12505 Components.reserve(TotalComponents);
12506 for (unsigned i = 0; i < TotalComponents; ++i) {
12507 Expr *AssociatedExprPr = Record.readExpr();
12508 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12509 Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12510 /*IsNonContiguous=*/false);
12511 }
12512 C->setComponents(Components, ListSizes);
12513}
12514
12515void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12516 C->setLParenLoc(Record.readSourceLocation());
12517 C->setColonLoc(Record.readSourceLocation());
12518 C->setAllocator(Record.readSubExpr());
12519 unsigned NumVars = C->varlist_size();
12520 SmallVector<Expr *, 16> Vars;
12521 Vars.reserve(NumVars);
12522 for (unsigned i = 0; i != NumVars; ++i)
12523 Vars.push_back(Record.readSubExpr());
12524 C->setVarRefs(Vars);
12525}
12526
12527void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12528 VisitOMPClauseWithPreInit(C);
12529 C->setNumTeams(Record.readSubExpr());
12530 C->setLParenLoc(Record.readSourceLocation());
12531}
12532
12533void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12534 VisitOMPClauseWithPreInit(C);
12535 C->setThreadLimit(Record.readSubExpr());
12536 C->setLParenLoc(Record.readSourceLocation());
12537}
12538
12539void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12540 VisitOMPClauseWithPreInit(C);
12541 C->setPriority(Record.readSubExpr());
12542 C->setLParenLoc(Record.readSourceLocation());
12543}
12544
12545void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12546 VisitOMPClauseWithPreInit(C);
12547 C->setGrainsize(Record.readSubExpr());
12548 C->setLParenLoc(Record.readSourceLocation());
12549}
12550
12551void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12552 VisitOMPClauseWithPreInit(C);
12553 C->setNumTasks(Record.readSubExpr());
12554 C->setLParenLoc(Record.readSourceLocation());
12555}
12556
12557void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12558 C->setHint(Record.readSubExpr());
12559 C->setLParenLoc(Record.readSourceLocation());
12560}
12561
12562void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12563 VisitOMPClauseWithPreInit(C);
12564 C->setDistScheduleKind(
12565 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12566 C->setChunkSize(Record.readSubExpr());
12567 C->setLParenLoc(Record.readSourceLocation());
12568 C->setDistScheduleKindLoc(Record.readSourceLocation());
12569 C->setCommaLoc(Record.readSourceLocation());
12570}
12571
12572void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12573 C->setDefaultmapKind(
12574 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12575 C->setDefaultmapModifier(
12576 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12577 C->setLParenLoc(Record.readSourceLocation());
12578 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12579 C->setDefaultmapKindLoc(Record.readSourceLocation());
12580}
12581
12582void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12583 C->setLParenLoc(Record.readSourceLocation());
12584 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12585 C->setMotionModifier(
12586 I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12587 C->setMotionModifierLoc(I, Record.readSourceLocation());
12588 }
12589 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12590 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12591 C->setColonLoc(Record.readSourceLocation());
12592 auto NumVars = C->varlist_size();
12593 auto UniqueDecls = C->getUniqueDeclarationsNum();
12594 auto TotalLists = C->getTotalComponentListNum();
12595 auto TotalComponents = C->getTotalComponentsNum();
12596
12597 SmallVector<Expr *, 16> Vars;
12598 Vars.reserve(NumVars);
12599 for (unsigned i = 0; i != NumVars; ++i)
12600 Vars.push_back(Record.readSubExpr());
12601 C->setVarRefs(Vars);
12602
12603 SmallVector<Expr *, 16> UDMappers;
12604 UDMappers.reserve(NumVars);
12605 for (unsigned I = 0; I < NumVars; ++I)
12606 UDMappers.push_back(Record.readSubExpr());
12607 C->setUDMapperRefs(UDMappers);
12608
12609 SmallVector<ValueDecl *, 16> Decls;
12610 Decls.reserve(UniqueDecls);
12611 for (unsigned i = 0; i < UniqueDecls; ++i)
12612 Decls.push_back(Record.readDeclAs<ValueDecl>());
12613 C->setUniqueDecls(Decls);
12614
12615 SmallVector<unsigned, 16> ListsPerDecl;
12616 ListsPerDecl.reserve(UniqueDecls);
12617 for (unsigned i = 0; i < UniqueDecls; ++i)
12618 ListsPerDecl.push_back(Record.readInt());
12619 C->setDeclNumLists(ListsPerDecl);
12620
12621 SmallVector<unsigned, 32> ListSizes;
12622 ListSizes.reserve(TotalLists);
12623 for (unsigned i = 0; i < TotalLists; ++i)
12624 ListSizes.push_back(Record.readInt());
12625 C->setComponentListSizes(ListSizes);
12626
12627 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12628 Components.reserve(TotalComponents);
12629 for (unsigned i = 0; i < TotalComponents; ++i) {
12630 Expr *AssociatedExprPr = Record.readSubExpr();
12631 bool IsNonContiguous = Record.readBool();
12632 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12633 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12634 }
12635 C->setComponents(Components, ListSizes);
12636}
12637
12638void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12639 C->setLParenLoc(Record.readSourceLocation());
12640 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12641 C->setMotionModifier(
12642 I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12643 C->setMotionModifierLoc(I, Record.readSourceLocation());
12644 }
12645 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12646 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12647 C->setColonLoc(Record.readSourceLocation());
12648 auto NumVars = C->varlist_size();
12649 auto UniqueDecls = C->getUniqueDeclarationsNum();
12650 auto TotalLists = C->getTotalComponentListNum();
12651 auto TotalComponents = C->getTotalComponentsNum();
12652
12653 SmallVector<Expr *, 16> Vars;
12654 Vars.reserve(NumVars);
12655 for (unsigned i = 0; i != NumVars; ++i)
12656 Vars.push_back(Record.readSubExpr());
12657 C->setVarRefs(Vars);
12658
12659 SmallVector<Expr *, 16> UDMappers;
12660 UDMappers.reserve(NumVars);
12661 for (unsigned I = 0; I < NumVars; ++I)
12662 UDMappers.push_back(Record.readSubExpr());
12663 C->setUDMapperRefs(UDMappers);
12664
12665 SmallVector<ValueDecl *, 16> Decls;
12666 Decls.reserve(UniqueDecls);
12667 for (unsigned i = 0; i < UniqueDecls; ++i)
12668 Decls.push_back(Record.readDeclAs<ValueDecl>());
12669 C->setUniqueDecls(Decls);
12670
12671 SmallVector<unsigned, 16> ListsPerDecl;
12672 ListsPerDecl.reserve(UniqueDecls);
12673 for (unsigned i = 0; i < UniqueDecls; ++i)
12674 ListsPerDecl.push_back(Record.readInt());
12675 C->setDeclNumLists(ListsPerDecl);
12676
12677 SmallVector<unsigned, 32> ListSizes;
12678 ListSizes.reserve(TotalLists);
12679 for (unsigned i = 0; i < TotalLists; ++i)
12680 ListSizes.push_back(Record.readInt());
12681 C->setComponentListSizes(ListSizes);
12682
12683 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12684 Components.reserve(TotalComponents);
12685 for (unsigned i = 0; i < TotalComponents; ++i) {
12686 Expr *AssociatedExprPr = Record.readSubExpr();
12687 bool IsNonContiguous = Record.readBool();
12688 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12689 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12690 }
12691 C->setComponents(Components, ListSizes);
12692}
12693
12694void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12695 C->setLParenLoc(Record.readSourceLocation());
12696 auto NumVars = C->varlist_size();
12697 auto UniqueDecls = C->getUniqueDeclarationsNum();
12698 auto TotalLists = C->getTotalComponentListNum();
12699 auto TotalComponents = C->getTotalComponentsNum();
12700
12701 SmallVector<Expr *, 16> Vars;
12702 Vars.reserve(NumVars);
12703 for (unsigned i = 0; i != NumVars; ++i)
12704 Vars.push_back(Record.readSubExpr());
12705 C->setVarRefs(Vars);
12706 Vars.clear();
12707 for (unsigned i = 0; i != NumVars; ++i)
12708 Vars.push_back(Record.readSubExpr());
12709 C->setPrivateCopies(Vars);
12710 Vars.clear();
12711 for (unsigned i = 0; i != NumVars; ++i)
12712 Vars.push_back(Record.readSubExpr());
12713 C->setInits(Vars);
12714
12715 SmallVector<ValueDecl *, 16> Decls;
12716 Decls.reserve(UniqueDecls);
12717 for (unsigned i = 0; i < UniqueDecls; ++i)
12718 Decls.push_back(Record.readDeclAs<ValueDecl>());
12719 C->setUniqueDecls(Decls);
12720
12721 SmallVector<unsigned, 16> ListsPerDecl;
12722 ListsPerDecl.reserve(UniqueDecls);
12723 for (unsigned i = 0; i < UniqueDecls; ++i)
12724 ListsPerDecl.push_back(Record.readInt());
12725 C->setDeclNumLists(ListsPerDecl);
12726
12727 SmallVector<unsigned, 32> ListSizes;
12728 ListSizes.reserve(TotalLists);
12729 for (unsigned i = 0; i < TotalLists; ++i)
12730 ListSizes.push_back(Record.readInt());
12731 C->setComponentListSizes(ListSizes);
12732
12733 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12734 Components.reserve(TotalComponents);
12735 for (unsigned i = 0; i < TotalComponents; ++i) {
12736 auto *AssociatedExprPr = Record.readSubExpr();
12737 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12738 Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12739 /*IsNonContiguous=*/false);
12740 }
12741 C->setComponents(Components, ListSizes);
12742}
12743
12744void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12745 C->setLParenLoc(Record.readSourceLocation());
12746 auto NumVars = C->varlist_size();
12747 auto UniqueDecls = C->getUniqueDeclarationsNum();
12748 auto TotalLists = C->getTotalComponentListNum();
12749 auto TotalComponents = C->getTotalComponentsNum();
12750
12751 SmallVector<Expr *, 16> Vars;
12752 Vars.reserve(NumVars);
12753 for (unsigned i = 0; i != NumVars; ++i)
12754 Vars.push_back(Record.readSubExpr());
12755 C->setVarRefs(Vars);
12756
12757 SmallVector<ValueDecl *, 16> Decls;
12758 Decls.reserve(UniqueDecls);
12759 for (unsigned i = 0; i < UniqueDecls; ++i)
12760 Decls.push_back(Record.readDeclAs<ValueDecl>());
12761 C->setUniqueDecls(Decls);
12762
12763 SmallVector<unsigned, 16> ListsPerDecl;
12764 ListsPerDecl.reserve(UniqueDecls);
12765 for (unsigned i = 0; i < UniqueDecls; ++i)
12766 ListsPerDecl.push_back(Record.readInt());
12767 C->setDeclNumLists(ListsPerDecl);
12768
12769 SmallVector<unsigned, 32> ListSizes;
12770 ListSizes.reserve(TotalLists);
12771 for (unsigned i = 0; i < TotalLists; ++i)
12772 ListSizes.push_back(Record.readInt());
12773 C->setComponentListSizes(ListSizes);
12774
12775 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12776 Components.reserve(TotalComponents);
12777 for (unsigned i = 0; i < TotalComponents; ++i) {
12778 Expr *AssociatedExpr = Record.readSubExpr();
12779 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12780 Components.emplace_back(AssociatedExpr, AssociatedDecl,
12781 /*IsNonContiguous*/ false);
12782 }
12783 C->setComponents(Components, ListSizes);
12784}
12785
12786void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12787 C->setLParenLoc(Record.readSourceLocation());
12788 auto NumVars = C->varlist_size();
12789 auto UniqueDecls = C->getUniqueDeclarationsNum();
12790 auto TotalLists = C->getTotalComponentListNum();
12791 auto TotalComponents = C->getTotalComponentsNum();
12792
12793 SmallVector<Expr *, 16> Vars;
12794 Vars.reserve(NumVars);
12795 for (unsigned i = 0; i != NumVars; ++i)
12796 Vars.push_back(Record.readSubExpr());
12797 C->setVarRefs(Vars);
12798 Vars.clear();
12799
12800 SmallVector<ValueDecl *, 16> Decls;
12801 Decls.reserve(UniqueDecls);
12802 for (unsigned i = 0; i < UniqueDecls; ++i)
12803 Decls.push_back(Record.readDeclAs<ValueDecl>());
12804 C->setUniqueDecls(Decls);
12805
12806 SmallVector<unsigned, 16> ListsPerDecl;
12807 ListsPerDecl.reserve(UniqueDecls);
12808 for (unsigned i = 0; i < UniqueDecls; ++i)
12809 ListsPerDecl.push_back(Record.readInt());
12810 C->setDeclNumLists(ListsPerDecl);
12811
12812 SmallVector<unsigned, 32> ListSizes;
12813 ListSizes.reserve(TotalLists);
12814 for (unsigned i = 0; i < TotalLists; ++i)
12815 ListSizes.push_back(Record.readInt());
12816 C->setComponentListSizes(ListSizes);
12817
12818 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12819 Components.reserve(TotalComponents);
12820 for (unsigned i = 0; i < TotalComponents; ++i) {
12821 Expr *AssociatedExpr = Record.readSubExpr();
12822 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12823 Components.emplace_back(AssociatedExpr, AssociatedDecl,
12824 /*IsNonContiguous=*/false);
12825 }
12826 C->setComponents(Components, ListSizes);
12827}
12828
12829void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12830 C->setLParenLoc(Record.readSourceLocation());
12831 unsigned NumVars = C->varlist_size();
12832 SmallVector<Expr *, 16> Vars;
12833 Vars.reserve(NumVars);
12834 for (unsigned i = 0; i != NumVars; ++i)
12835 Vars.push_back(Record.readSubExpr());
12836 C->setVarRefs(Vars);
12837 Vars.clear();
12838 Vars.reserve(NumVars);
12839 for (unsigned i = 0; i != NumVars; ++i)
12840 Vars.push_back(Record.readSubExpr());
12841 C->setPrivateRefs(Vars);
12842}
12843
12844void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12845 C->setLParenLoc(Record.readSourceLocation());
12846 unsigned NumVars = C->varlist_size();
12847 SmallVector<Expr *, 16> Vars;
12848 Vars.reserve(NumVars);
12849 for (unsigned i = 0; i != NumVars; ++i)
12850 Vars.push_back(Record.readSubExpr());
12851 C->setVarRefs(Vars);
12852}
12853
12854void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12855 C->setLParenLoc(Record.readSourceLocation());
12856 unsigned NumVars = C->varlist_size();
12857 SmallVector<Expr *, 16> Vars;
12858 Vars.reserve(NumVars);
12859 for (unsigned i = 0; i != NumVars; ++i)
12860 Vars.push_back(Record.readSubExpr());
12861 C->setVarRefs(Vars);
12862}
12863
12864void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12865 C->setLParenLoc(Record.readSourceLocation());
12866 unsigned NumOfAllocators = C->getNumberOfAllocators();
12867 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12868 Data.reserve(NumOfAllocators);
12869 for (unsigned I = 0; I != NumOfAllocators; ++I) {
12870 OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12871 D.Allocator = Record.readSubExpr();
12872 D.AllocatorTraits = Record.readSubExpr();
12873 D.LParenLoc = Record.readSourceLocation();
12874 D.RParenLoc = Record.readSourceLocation();
12875 }
12876 C->setAllocatorsData(Data);
12877}
12878
12879void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12880 C->setLParenLoc(Record.readSourceLocation());
12881 C->setModifier(Record.readSubExpr());
12882 C->setColonLoc(Record.readSourceLocation());
12883 unsigned NumOfLocators = C->varlist_size();
12884 SmallVector<Expr *, 4> Locators;
12885 Locators.reserve(NumOfLocators);
12886 for (unsigned I = 0; I != NumOfLocators; ++I)
12887 Locators.push_back(Record.readSubExpr());
12888 C->setVarRefs(Locators);
12889}
12890
12891void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12892 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12893 C->setLParenLoc(Record.readSourceLocation());
12894 C->setKindKwLoc(Record.readSourceLocation());
12895}
12896
12897OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12898 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12899 TI.Sets.resize(readUInt32());
12900 for (auto &Set : TI.Sets) {
12901 Set.Kind = readEnum<llvm::omp::TraitSet>();
12902 Set.Selectors.resize(readUInt32());
12903 for (auto &Selector : Set.Selectors) {
12904 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12905 Selector.ScoreOrCondition = nullptr;
12906 if (readBool())
12907 Selector.ScoreOrCondition = readExprRef();
12908 Selector.Properties.resize(readUInt32());
12909 for (auto &Property : Selector.Properties)
12910 Property.Kind = readEnum<llvm::omp::TraitProperty>();
12911 }
12912 }
12913 return &TI;
12914}
12915
12916void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
12917 if (!Data)
12918 return;
12919 if (Reader->ReadingKind == ASTReader::Read_Stmt) {
12920 // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
12921 skipInts(3);
12922 }
12923 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
12924 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
12925 Clauses[I] = readOMPClause();
12926 Data->setClauses(Clauses);
12927 if (Data->hasAssociatedStmt())
12928 Data->setAssociatedStmt(readStmt());
12929 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
12930 Data->getChildren()[I] = readStmt();
12931}
12932